navigation

Java Tips & Tricks

Java Tips & Tricks

by
May 21, 2014

Paths and JARs

These are just some tips and tricks I used or read last month that I’d like to share. This list does not contain silver bullets and it doesn’t pretend to be complete or be an absolute truth. It’s just a list that I hope may be helpful for someone.

1. Paths

To Build big enterprise applications and integrate them everywhere easy we need to use relative paths. So let’s see different ways to do it.

Get path to project directory in Java

 String dir = System.getProperty("user.dir") 

But we must not forget that the user.dir property is the current working directory where we are running Java.

Get path to file in Java

File file = new File("resources/.././filename.txt");
	    String path = file.getPath();
	    String absolutePath = file.getAbsolutePath();
	    try {
		  String canonicalPath = file.getCanonicalPath();
	    } catch (IOException e) {
		  e.printStackTrace();
	    }

The differences between these three methods are:

  • getPath() gets the path string that the File object was constructed with, and it may be the relative current directory. Output looks something like this:
resources\..\.\filename.txt
  • GetAbsolutePath() gets the path string after resolving it against the current directory if it’s relative, resulting in a fully qualified path. Output looks something like this:
D:\workspace\PathsTest\resources\..\.\filename.txt
  • GetCanonicalPath() gets the path string after resolving any relative path against the current directory, and removes any relative pathing (. and ..), and any file system links to return a path which the file system considers the canonical means to reference the file system object to which it points. Output looks something like this:
D:\workspace\PathsTest\filename.txt

Get path using ClassLoader in Java

String path = getClass().getClassLoader().getResource(".").getPath();

Output will be:

/D:/workspace/PathsTest/bin/

Get path inside XML file

To get the path to the root directory of a project is enough to use “.”. For example:

value
./resources/filename.txt
/value

 

2. Refactoring of a JAR file

Sometimes we need to edit some JARs to make our application works. The reasons to do this could be different – there is an error in the JAR or just for our case we need a little different implementation that couldn’t be override and etc. So to edit JAR we need go through some steps:

  • Extracted the files using a program as WinRAR or 7zip. How we know JAR files are fundamental archive files, built on the ZIP file format and have the .jar file extension.
  • Made changes to extracted files. We usually have to change the code in some java files, but JARs contain only compiled java files – .class files. To edit a class file, we need a decompiler. After we decompile the class file we can easy edit the newly generated java file. In the end, we just have to compile this new file.
  • Opened the original JAR file with WinRAR.
  • Used the ADD button to replace the files that we modified.

3. Read file from a JAR file

Everyone knows how to read a file. To read a file from JAR file is a little bit different. For example when we read a file from our hard drive we use code like this:

FileReader inputReader = new FileReader("filename.txt");
		BufferedReader reader = new BufferedReader(inputReader);
		String line = null;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
		}
		reader.close();

If file is inside JAR file and the JAR is on the classpath we use code like this:

InputStream inputStream = MyClass.class.getResourceAsStream("/filename.txt");
		InputStreamReader inputReader = new InputStreamReader(inputStream);
		BufferedReader reader = new BufferedReader(inputReader);
		String line = null;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
		}
		reader.close();

The nice thing about this is it works perfectly whether the files are in a JAR or not. If they’re not, then it works as long as the parent of the data directory is on your classpath.If file is inside JAR file and the JAR is NOT on the classpath we use code like this:

URL url = new URL("jar:file:/absolute/location/of/yourJar.jar/filename.txt");
		InputStream inputStream = url.openStream();

 

It’s simply not possible to write to a file inside JAR file.

What do you think about those Tips & Tricks – are they useful?
Can you share with us some tips about Java that you use ?
What do you think about Java 8? 

More about how to read a Jar file and the Difference between WAR, EAR and JAR files in Java Tips & Tricks vol 2!

Boris Velichkov

Java EE and Oracle Developer

More Posts - Website

Follow Me:
TwitterLinkedIn

Do you want more great blogs like this?

Subscribe for Dreamix Blog now!