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
[java] String dir = System.getProperty("user.dir") [/java]
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
[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();
}
[/java]
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:
[java]
resources…filename.txt
[/java]
- 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:
[java]
D:workspacePathsTestresources…filename.txt
[/java]
- 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:
[java]
D:workspacePathsTestfilename.txt
[/java]
Get path using ClassLoader in Java
[java]
String path = getClass().getClassLoader().getResource(".").getPath();
[/java]
Output will be:
[java]
/D:/workspace/PathsTest/bin/
[/java]
Get path inside XML file
To get the path to the root directory of a project is enough to use “.”. For example:
[java]
value
./resources/filename.txt
/value
[/java]
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:
[java]
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();
[/java]
If file is inside JAR file and the JAR is on the classpath we use code like this:
[java]
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();
[/java]
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:
[java]
URL url = new URL("jar:file:/absolute/location/of/yourJar.jar/filename.txt");
InputStream inputStream = url.openStream();
[/java]
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!