Help with writing to the JWS cache directory?

We have a java application named "foo" that we would like to deploy via Webstart.The original version of foo is packaged into a single foo.jar archive. Foo creates a textfile "info.txt" outside of foo.jar. "Info.txt" stores information that we would like to remain constant for each client machine, which is why we are not wrapping this info inside foo.jar b/c "foo.jar" is overwritten each time a new version is available on the server.

When running "java -jar foo.jar" outside of Webstart, foo works correctly and generates "info.txt" in the same directory where foo.jar is located. The current directory is determined by this code snippet--

File infoFile = new File ( "info.txt");

However, when we deploy "foo.jar" through Webstart, foo fails to work properly on the client machine b/c it is unable to write "info.txt" in the directory where "foo.jar" has been downloaded and cached. Note that we have signed foo.jar properly and it is allowed to write to the local filesystem. But instead of generating "info.txt" in the same location as "foo.jar", it creates "info.txt" on the client desktop causing problems with the running application.

Can anyone tell us how to capture the client's cache directory location during Webstart download so that "info.txt" can be created and placed in that directory?

[1377 byte] By [JH3] at [2007-9-26 3:11:18]
# 1

Hi,

you shouldn't put your file into the cache directory. Use user.home/<your_app>/info.txt instead. or consider using a muffin, for example. You can find more info at the Unofficical Web Start/JNLP FAQ at http://www.geocities.com/vamp201/jwsfaq.html

This is just my opinion and you can do of couse as you please.

- Gerald

geraldb at 2007-6-29 11:18:37 > top of Java-index,Desktop,Deploying...
# 2
There is a way to get at the cache directory, but I agree with Gerald - it is unnecessarily complicated, and user.home works fine.
j9archer at 2007-6-29 11:18:37 > top of Java-index,Desktop,Deploying...
# 3
Hi, thanks to both of you. We've implemented your suggestion and the app is working well.-JH
JH3 at 2007-6-29 11:18:37 > top of Java-index,Desktop,Deploying...
# 4
Hello,please tell witch way you realy meanthenks.
jasineri at 2007-6-29 11:18:37 > top of Java-index,Desktop,Deploying...
# 5
> There is a way to get at the cache directory, but I> agree with Gerald - it is unnecessarily complicated,> and user.home works fine.Just out of curiosity, how DO you programmatically get the location of the cache directory? Thanks!
645 at 2007-6-29 11:18:37 > top of Java-index,Desktop,Deploying...
# 6
Nobody knows how to retrieve the JWS cache directory?
arnaud.java at 2007-6-29 11:18:37 > top of Java-index,Desktop,Deploying...
# 7

you can find the path to the cache directory by getting a jar url to one of your resources in the cache:

URL jarURL = this.getClass().getClassLoader().getResource("MyClass.class");

String jarFileStr = jarURL.getFile();

1.) first remove the "!/MyClass.class":

jarFileStr = jarFileStr.substring(0, jarFileStr.length() - "!/MyClass.class".length());

2.) then remove the jar://file: in front:

if (jarFileStr.startsWith("jar://file:") { jarFileStr = jarFileStr.substr(11); }

3.) may need to transform %20 to spaces, and forward slashes to File.seperator

jarFileStr.replace("%20", " ");

This gives me a string value of:

file:C:/Documents%20and%20Settings/glyn/.javaws/cache/http/Dgwwinxp/P80/DMlive/DMjava/RMBBClient.jar

When I try to do:

File jarFile = new File(jarFileStr);

JarFile jar = new JarFile(jarFile);

I get java.util.zip.ZipException: The filename, directory name, or volume label syntax is incorrect.

Has anyone got this method of getting the jar file to work?

Thanks

Glyn

Re: Java Web Start getResource() issue

Author: dietz333

In Reply To: Re: Java Web Start getResource() issue Sep 29, 2003 10:18 AM

Reply 5 of 8

Glenn:

the string - file:C:/Documents%20and%20Settings/glyn/... is not legal path.

1.) first you need to strip off the "file:"

2.) you need to change the '/' to '\' (on windows).

use str.replaceAll("/", File.separator)

/dietz

Re: Java Web Start getResource() issue

Author: gdub

In Reply To: Re: Java Web Start getResource() issue Sep 30, 2003 9:29 AM

Reply 6 of 8

Thanks for the reply.

I changed the line to

jarFileStr = jarFileStr.substring("file:".length(), jarFileStr.length() - "!/com/xxxx".length());

File file = new File(jarFileStr);

The System.out of file is

C:\Documents%20and%20Settings\glyn\.javaws\cache\http\Dgwwinxp\P80\DMlive\DMjava\RMMyApp.jar but isExists() returns false on the file.

I guess it doesn't like the %20 etc. because that it the path to the file alright..

Re: Java Web Start getResource() issue

Author: gdub

In Reply To: Re: Java Web Start getResource() issue Sep 30, 2003 9:33 AM

Reply 7 of 8

This works:

jarFileStr = jarFileStr.substring("file:".length(), jarFileStr.length() - "!/com/xxxx".length());

jarFileStr.replaceAll("%20", " ");

File file = new File(jarFileStr);

Not very pretty. But it will do me for now.

Thanks for your help.

Re: Java Web Start getResource() issue

Author: josephconder

In Reply To: Re: Java Web Start getResource() issue Oct 3, 2003 1:43 PM

Reply 8 of 8

I ran into this problem as well. The string is URL encoded (hence the %20=space).

You could use URLDecoder.decode(jarFileStr) to get the same result (and not worry about any other encoded characters).

dietz333 at 2007-6-29 11:18:37 > top of Java-index,Desktop,Deploying...
# 8

you can find the path to the cache directory by getting a jar url to one of your resources in the cache:

URL jarURL = this.getClass().getClassLoader().getResource("MyClass.class");

String jarFileStr = jarURL.getFile();

1.) first remove the "!/MyClass.class":

jarFileStr = jarFileStr.substring(0, jarFileStr.length() - "!/MyClass.class".length());

2.) then remove the jar://file: in front:

if (jarFileStr.startsWith("jar://file:") { jarFileStr = jarFileStr.substr(11); }

3.) may need to transform %20 to spaces, and forward slashes to File.seperator

jarFileStr = jarFileStr.replaceAll("%20", " ");

jarFileStr = JarFileStr.replace("\/", File.separator);

or just use URLDecoder.decode(jarFileStr) to get the same result (and not worry about any other encoded characters).

now you have a path to your jar file in the cache. to translate to root of cache format is:

ROOT_OF_CACHE/UUUU/PVVVV/DMXXXX/[DMYYYY]*/RMZZZZ where

UUUU is the protocol in your codebase (http, https, file)

VVVV is port number of codebase (or 80 if none listed)

XXXX is the server name of your codebase.

YYYY is a directory name in either your codebase or jar href.

(repeat as needed)

ZZZZ is your jar files name.

hope this helps.

/Dietz

dietz333 at 2007-6-29 11:18:37 > top of Java-index,Desktop,Deploying...
# 9

But you don't want to get the cache directory!!

It is not at all out of scope of the JNLP specs that a cache directory does not exist and, say, is actually an SQL database... (think for example of people running a large pool of computers with mobile users).

- If you want to store small information, use muffins, they're really perfect. I think they're currently limited to 4Mb...

- If you want to store large information, use an installer and there you get a space to install something in which you might feel free to modify (it's actually in .javaws directory anyways).

- And for temporary things, you can always use the other services...

It's a shame there's no other JNLP implementation widely used, this kind of thing shouldn't be wished.

Paul

LeManuel at 2007-6-29 11:18:37 > top of Java-index,Desktop,Deploying...