Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use File.separator for a jar file resource?

Tags:

java

jar

I am trying to read a property file which is located within a jar file. I want to use File.separator as the application will run on multiple platforms. I am constructing the path as follows:

jarFilePath="jar:file:"+jarFile.getAbsolutePath()+"!"+jarPropertyFilePath; //jarPropertyFilePath is getting loaded from a class which is storing Constant JAR_PROPERTY_FILE_PATH.
fileURL= new URL(jarFilePath);

However, the above works only when jarPropertyFilePath (fixed location of property file within the jar) is given as :

Case 1:

public static final String JAR_PROPERTY_FILE_PATH = "/manifest/patchControl.properties";

Case 2:

If I use File.separator as follows, it gives java.net.MalformedURLException: no !/ in spec

public static final String JAR_PROPERTY_FILE_PATH = File.separator+"manifest"+File.separator+"patchControl.properties";     

In Case 1 (workds fine), the final URL when debugged was as follows:

jar:file:C:\MyWorkspace\FalconPatchInstaller\.\patches\DS661JDK1.8.jar!/manifest/patchControl.properties

In Case 2(not working), the final URL when debugged was as follows:

jar:file:C:\MyWorkspace\FalconPatchInstaller\.\patches\DS661JDK1.8.jar!\manifest\patchControl.properties

How can I use the File.separator for the JAR_PROPERTY_FILE_PATH ?

like image 819
Crusaderpyro Avatar asked Jul 15 '14 02:07

Crusaderpyro


1 Answers

I am trying to read a property file which is located within a jar file. I want to use File.separator as the application will run on multiple platforms

No, you don't want to use File.separator at all. The defined path separator in JAR files is /. It is not platform-dependent.

NB You can use / within filenames as well on all platforms in Java. But this isn't a file name, it is a resource name, and the thing it names isn't a file, it is a resource.

like image 187
user207421 Avatar answered Oct 11 '22 12:10

user207421