Get the Current Working Directory in Java

How do you get the current working directory in Java? There are a few different ways of getting the the current working directory. System Property user.dir The System class contains properties that describes the configuration of the current working environment. The property that returns the current working directory is “user.dir”. String userDir = System.getProperty(“user.dir”);System.out.println(“The current …

Continue reading

The Complete Maven pom.xml Structure Documentation

Maven is a tool for building Java applications. It has a pom.xml file that contains all the build instructions as well as a list of project dependencies. The pom.xml file has a bewildering amount of configurable options. The complete Maven pom.xml structure documentation (project descriptor file) is here on the Maven web site. This page …

Continue reading

Want to use Spring 3.0 milestone release from Maven?

I wanted to add the Spring 3.0 M4 milestone early access release to a Maven project to test out some of the new features. However SpringĀ  milestone releases are not readily available in the standard public Maven repositories, so what do you do? For the full explanation check out the link to “Spring/Maven Repositories” link …

Continue reading

JUnit 4 error: reference to assertEquals is ambiguous

This is a somewhat confusing compilation failure that sometimes happen when you write unit tests using JUnit 4. This is an example code snippet that produces this error (result.getValue() returns an Integer object): assertEquals(12345, result.getValue()); And when you try to compile your project it produces a compilation error like this: /projects/myapp/src/test/java/org/myapp/MyTest.java:[88,8] reference to assertEquals is …

Continue reading

Maven, Log4j and javax.jms

Log4j version 1.2.15 added features which has new dependencies on sun and javax packages. When you try to build your project using Maven and log4j 1.2.15 you will see this: [INFO] Unable to find resource ‘com.sun.jmx:jmxri:jar:1.2.1’ in repository central (http://repo1.maven.org/maven2) [INFO] Unable to find resource ‘com.sun.jdmk:jmxtools:jar:1.2.1’ in repository central (http://repo1.maven.org/maven2) [INFO] Unable to find resource …

Continue reading

How to read environment variables in Java

You can set environment variables or pass them in using the -Dvariablename syntax SET logfile=/location/to/my/log/file.log or java -Dlogfile=/location/to/my/log/file.log HelloWorld When you want to access these environment variables from your Java source file you can use Java 1.5 and newer: String logfileLocation = System.getenv(“logfile”); Java 1.4 and older: String logfileLocation = System.getProperty(“logfile”);

Continue reading

What jar file does a Java class belong to?

I found this very useful web site that allows you to search what JAR file a specific Java class belongs to. This has proven quite useful to me over the last couple of days when I had to deal with NoClassDefFoundError and ClassNotFoundException problems. You can just enter the name of the class or JAR …

Continue reading