5

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 ?

Crusaderpyro
  • 2,163
  • 5
  • 29
  • 53
  • Is the jar file that you are attempting to read in the classpath of the application that is parsing the file? If the jar file is in your classpath you are taking the wrong approach. See [accessing resources](http://docs.oracle.com/javase/6/docs/technotes/guides/lang/resources.html). – javajon Jul 15 '14 at 02:56
  • No its not on the classpath. Its at a separate location given by jarFile.getAbsolutePath(). I guess if it was on the class path I would be using getResourceStream method. – Crusaderpyro Jul 15 '14 at 02:58
  • You don't need File.separator as ['/' works on all platforms](http://stackoverflow.com/questions/2417485/file-separator-vs-slash-in-paths)) including Windows. Your answer may be [here](http://stackoverflow.com/questions/8014099/how-do-i-convert-a-jarfile-uri-to-the-path-of-jar-file). Otherwise, post some code and follow [MCVE](http://stackoverflow.com/help/mcve) – javajon Jul 15 '14 at 03:18
  • 1
    @javajon Your first link is about files in the file system, not resources in a JAR file. You're right, but for the wrong reason. – user207421 Jul 15 '14 at 06:49

1 Answers1

12

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.

user207421
  • 305,947
  • 44
  • 307
  • 483