Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31123 > unrolled thread

Executable JAR files and writing to a text file.

Started byDoug Mika <dougmmika@gmail.com>
First post2015-02-12 09:40 -0800
Last post2015-02-14 22:31 -0500
Articles 4 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Executable JAR files and writing to a text file. Doug Mika <dougmmika@gmail.com> - 2015-02-12 09:40 -0800
    Re: Executable JAR files and writing to a text file. Jeff Higgins <jeff@invalid.invalid> - 2015-02-12 19:22 -0500
    Re: Executable JAR files and writing to a text file. Nigel Wade <nmw@ion.le.ac.uk> - 2015-02-13 10:01 +0000
    Re: Executable JAR files and writing to a text file. Arne Vajhøj <arne@vajhoej.dk> - 2015-02-14 22:31 -0500

#31123 — Executable JAR files and writing to a text file.

FromDoug Mika <dougmmika@gmail.com>
Date2015-02-12 09:40 -0800
SubjectExecutable JAR files and writing to a text file.
Message-ID<5efb672e-7a66-4db0-af62-42fc48cd59f6@googlegroups.com>
I have an application in which it writes to a text file at a specific directory location.  I run the program and it runs fine! Now I create an executable JAR file.  Should the jar file work fine if the program needs to write to a text file in ../resources/HighScore.txt?

Thanks again
Doug

[toc] | [next] | [standalone]


#31128

FromJeff Higgins <jeff@invalid.invalid>
Date2015-02-12 19:22 -0500
Message-ID<mbjgbg$omf$1@dont-email.me>
In reply to#31123
On 02/12/2015 12:40 PM, Doug Mika wrote:
> I have an application in which it writes to a text file at
> a specific directory location.
> I run the program and it runs fine! Now I create an executable JAR file.
> Should the jar file work fine if the program needs to write to a
> text file in ../resources/HighScore.txt?

It seems to me that you are hoping to access a text file that is
external to your jar, and that this external text file is located
in a directory relative to your jar. If this is correct, then
there seems to be some gotchas involved and I'm not familiar
enough with the api to offer concrete advice. But there is some
discussion on stackoverflow that might help.
<http://stackoverflow.com/questions/8775303/read-properties-file-outside-jar-file>
Is one such discussion. Probably more, you might try searching there.



[toc] | [prev] | [next] | [standalone]


#31129

FromNigel Wade <nmw@ion.le.ac.uk>
Date2015-02-13 10:01 +0000
Message-ID<ck60cdF2blfU1@mid.individual.net>
In reply to#31123
On 12/02/15 17:40, Doug Mika wrote:
> I have an application in which it writes to a text file at a specific directory location.  I run the program and it runs fine! Now I create an executable JAR file.  Should the jar file work fine if the program needs to write to a text file in ../resources/HighScore.txt?
>

Yes, provided the /relative/ location you are using (it is not a specific location) actually corresponds to the file you 
want to access.

In addition to what Jeff has suggested, you could look at the Java Preferences class. It may provide an alternative 
solution.

[toc] | [prev] | [next] | [standalone]


#31132

FromArne Vajhøj <arne@vajhoej.dk>
Date2015-02-14 22:31 -0500
Message-ID<54e01313$0$287$14726298@news.sunsite.dk>
In reply to#31123
On 2/12/2015 12:40 PM, Doug Mika wrote:
> I have an application in which it writes to a text file at a specific
> directory location.  I run the program and it runs fine! Now I create
> an executable JAR file.  Should the jar file work fine if the program
> needs to write to a text file in ../resources/HighScore.txt?

One potential problem is that ../resources/HighScore.txt is
not relative to the location of the jar file but relative to
the default dir of the process running the program.

For illustration try run this program with a default dir
that is different from code location dir.

import java.io.File;

public class WhatDir {
	public static void main(String[] args) throws Exception {
		String relfnm = "../subdir/filename.txt";
		String p1 = new File(relfnm).getAbsolutePath();
		System.out.println(p1);
		String p2 = new File(relfnm).getCanonicalPath();
		System.out.println(p2);
		String p3 = new 
File(WhatDir.class.getProtectionDomain().getCodeSource().getLocation().getPath() 
+ relfnm).getAbsolutePath();
		System.out.println(p3);
		String p4 = new 
File(WhatDir.class.getProtectionDomain().getCodeSource().getLocation().getPath() 
+ relfnm).getCanonicalPath();
		System.out.println(p4);
	}
}

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web