java project need assistance

From what directory are you running the program? "user.dir" will vary depending on what directory you're in on the command line when you invoke java.

If my working directory is the default (C:\Users\Joseph Weaver) it saves the files in the C:\Users directory. If I change my directory to that of the flash drive it saves it to the root directory of the flash drive. I need it to always save the file in the same directory as the .jar file
 
Standalone run from the command line using ">java Dirtest"

JAR from command line using ">java -jar Dirtest.jar"

and Double clicked from explorer, and in all cases it gave me the correct output
Well, it should. In all three cases, the working directory and the directory containing the .jar are one and the same. They're probably going to differ with something like this:

C:\x\y\z>java -jar c:/Dirtest.jar
 
Well, it should. In all three cases, the working directory and the directory containing the .jar are one and the same. They're probably going to differ with something like this:

C:\x\y\z>java -jar c:/Dirtest.jar

I know, that's why I asked what the command line was that he ran it from :D. I was just demonstrating that the user.dir key does work.

OP needs to post prompt he's running from and the command he's running. Something is wonky here.
 
[King_Weaver]
> I need it to always save the file in the same directory as the .jar file

Believe it or not, what you're asking is very difficult to do in pure java. There's a thread about it on the Sun forums: http://forums.sun.com/thread.jspa?threadID=611734&messageID=3370212

It seems that you can find the location of the JAR file containing the main class inside a running program with:
Code:
File jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI());

Ugly, right? Anyway, once you have jarFile, you can specify a file in the same directory with:
Code:
File fileInJarDir = new File(jarFile.getParentFile(), "fileInJarDir.txt");

Then you can pass that file to one of FileWriter's constructors.
 
Ok, I mistakenly assumed before that you wanted to use a file INSIDE the jar... my bad.

But I am running your code, and it is saving the files in whatever directory I am running the jar from.

tried 3 diffferent computers, 2 mac 1 XP, and a bunch of random directories / drives.


How are you building your jar file?
 
Im building it in netbeans, heres some code right now its giving me an error "non-static method getclass() cannot be referenced from static context" the red text is the line that gives this error. I appreciate all the help guys. :) I feel like Im almost there
 
Im building it in netbeans, heres some code right now its giving me an error "non-static method getclass() cannot be referenced from static context" the red text is the line that gives this error. I appreciate all the help guys. :) I feel like Im almost there

Code:
       File jarfilelocation = new File([u][b]new Main[/b][/u].getClass().getProtectionDomain().getCodeSource().getLocation().toURI());        
      
    }

This happened because of the wonky way you wrote your code (I won't go in to it as other have already done so). There is your fix.

Also please post the command prompt and command you have been running the jar from. The user.dir key should be all you need, something is not right here.
 
ok I added the "new Main." to the begining, Im no longer getting the previous error but now it says cannot find symbol .getclass() , do I need to import something?
 
Also please post the command prompt and command you have been running the jar from. The user.dir key should be all you need, something is not right here.

I if run

C:\Users\Joseph Weaver>java -jar "G:\Joseph Weaver COP3804 HW 1\dist\Joseph_Weaver_COP3804_HW_1.jar"

it puts all the files in the C:\Users folder

If I run

G:\>java -jar "G:\Joseph Weaver COP3804 HW 1\dist\Joseph_Weaver_COP3804_HW_1.jar"

It puts all the files in the root directory of my flash drive (G:)

what i need it to do is put all the files in "X:\Joseph Weaver COP3804 HW 1\dist" , X being the drive letter the os assigns to the flash drive.
 
yep you were right this is what did it

Code:
BufferedWriter output = new BufferedWriter(
                new FileWriter(System.getProperty("user.dir") +
                "Joseph Weaver COP3804 HW 1\\dist\\"+File.separator + 
                File.separator + lastname +" " + semester + ".txt"));
 
I if run

C:\Users\Joseph Weaver>java -jar "G:\Joseph Weaver COP3804 HW 1\dist\Joseph_Weaver_COP3804_HW_1.jar"

it puts all the files in the C:\Users folder

If I run

G:\>java -jar "G:\Joseph Weaver COP3804 HW 1\dist\Joseph_Weaver_COP3804_HW_1.jar"

It puts all the files in the root directory of my flash drive (G:)

what i need it to do is put all the files in "X:\Joseph Weaver COP3804 HW 1\dist" , X being the drive letter the os assigns to the flash drive.

I knew you were doing something wonky!

When you pass in a relative path (i.e. "test.txt") it is going to create the file in the directory you were in when you ran the jar, regardless of where the jar itself actually is. The easiest way to get around this (and this is often a requirement of console utilities of all languages) is that you run the jar from the directory the jar is in, which is not an unreasonable precondition to put in your documentation.

I would advise against using absolute paths as you were saying, just go back to using the filename without a qualifying path and make sure you always run the jar from inside its directory (i.e cd to the directory before you run it). Now that you understand the restrictions involved with relative paths, go back to using this instead.
Code:
BufferedWriter output = new BufferedWriter(
                new FileWriter(lastname +" " + semester + ".txt"));
 
For what it's worth, the way to fix the code I suggested is:
Code:
File jarFile = new File([b]Main.class[/b].getProtectionDomain().getCodeSource().getLocation().toURI());

I forgot that you structured your program in a goofy way such that all methods were static.
 
Back
Top