Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
JAR File reading problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

JAR File reading problem



Hello all!

I'm writing a method for my application that reads a file inside a jar bundle:

	private void readFile() throws IOException, MalformedURLException {
		//Get the current directory:
		File dir = new File(".");
		//Build URL containing path to the JAR file:
		URL cacheurl = new URL("jar:file:" + dir.getCanonicalPath() +
							  "/GlossTerm.jar!/");
		//Build URL with path to file in the JAR file:
		URL fileurl = new URL(cacheurl, "/data.txt");
		JarURLConnection connection =
			(JarURLConnection) fileurl.openConnection();
		connection.setDoInput(true);
		connection.setDoOutput(false);
		connection.connect();

		InputStream inps = connection.getInputStream();

		BufferedReader in = new BufferedReader(new InputStreamReader(inps));

The problem is that this works great when the user from the terminal
executes the JAR file from the same directory that it current resides
e.g. java -jar jarName.jar. However, when the user doesn't execute the
jar file from the same directory that the jar file is in e.g. java
-jar /DirName1/DirName2/jarName.jar then I run into a ZipExeception
error where the file can't be found. Any help on this would be most
welcomed!

Thanks,

Saad.
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.