1

I exported my java project and ran it. At first it didnt work when I double clicked but upon some research I came across this solution

What I did was to create a batch file, jax.bat, saying: @java -jar %1 and I put this in my java/bin folder. I then associate .jar-files with jax.bat.

After that the problem seemed to be solved. However, when I tried to run it on my friends computer I found that when running from the command prompt it would write to the file as expected, but when it was run by double clicking it did not. Here's the code for the filewriter

File writeTo = new File("Destinations.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(writeTo),32768);
//Some lines later
bw.write("The price is $"+ prices.get(lowest)+" travelling to "+ places.get(i)+" on "+ month+"/"+day);
bw.newLine();
          }
bw.close();

Edit: I tried adding the String workingDir... and running it on his computer. The message appeared, but said the file was in Windows/System32

Edit2: I built my jar file simply by exporting a runnable jar from eclipse.

tshepang
  • 12,111
  • 21
  • 91
  • 136
DavidR
  • 35
  • 4
  • How did you build your Jar? – Ravi K Thapliyal Jul 08 '13 at 01:48
  • 1
    i dont know much about java . But this seems to be a close question asked before : http://stackoverflow.com/questions/5827164/why-does-my-jar-file-execute-at-cmd-but-not-on-double-click?rq=1 See if this helps . – rockstar Jul 08 '13 at 01:50

1 Answers1

0

When you run the JAR via double-click it's probably doing something weird to the working directory. Your file is written to the current working directory (since you didn't specify a path), and that might not be where you're looking. You can do something like this to get the current working directory:

// Get the current working directory
String workingDir = System.getProperty("user.dir");
// Display workingDir in a Swing message dialog window
JOptionPane.showMessageDialog(null, workingDir);

Destinations.txt should appear in the directory indicated by workingDir.

On the other hand, if the problem is just that the program isn't actually running correctly when you launch it via double-click, then it should be obvious when the message dialog doesn't appear.

DaoWen
  • 32,589
  • 6
  • 74
  • 101
  • 1
    on his computer it said that the directory was System32. However it has worked on a third computer we tried. Any ideas? – DavidR Jul 08 '13 at 04:29
  • Try running [Jarfix (johann.loefflmann.net/jarfix)](http://johann.loefflmann.net/jarfix) and see if that fixes the working directory issue. – DaoWen Jul 08 '13 at 06:35