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

jar, java

Solution

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.

Problem

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 ?

Original source

Related problems