0

The code in question is simple something along the lines of

File f = new File("Testfile.txt");
f.createFile();

the file would later be written into. This code is in an executable jar file. When it is executet via "java -jar jarname.jar" it works fine, but when it's being launched via normal doubleclick, it does not work. Nothing happens. So far this seems to be the case because the file created somehow does not actualy denote a file in the directory the jar file is in but that is just an assumption. For short: how can I create and edit a file from a runnable jar that is being run via doubleclick?

Thanks in advance

Zain
  • 95
  • 1
  • 8
  • You could make it so it has a file not found throw exception and have it print a message listing the directory searched and the file it was searching for. This isn't an answer but a step in finding a possible solution. – DarkJade Apr 13 '16 at 19:57
  • @DarkJade There is no exception because everything works - just not the way it's supposed to :) – Zain Apr 14 '16 at 16:30

2 Answers2

0

So far this seems to be the case because the file created somehow does not actualy denote a file in the directory the jar file is in but that is just an assumption.

Correct. It doesn't. It represents a file in the current working directory. What that is when you double-click depends entirely on how the double-click is set up. Possibly the root directory.

For short: how can I create and edit a file from a runnable jar that is being run via doubleclick?

Why? Why create the file before it's written into? Don't do this. The time to create the file is when you create your file output stream or writer. If that fails you get an exception too, which is more helpful than the Boolean returned by File.createFile(), which in any case you are ignoring.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • "create and edit" as in "I want to edit a File but also be able to edit a File". For example for mkdir and listing files. The writing into a File part comes later :) – Zain Apr 14 '16 at 17:00
  • Why do you want to list empty files? – user207421 Apr 14 '16 at 21:16
  • Uuuuh been some time but i think the folder and the files therein were accessed manually, too. – Zain Nov 13 '16 at 14:42
0

This issue is of different nature: The files DO get created - the problem is that they're created in the executing users home directory where you can easily miss them (and don't want them). The solution is to get the current Locatin of the executed jar file as discussed here: How to get the path of a running JAR file?

I hope this helps, Cheers!

Community
  • 1
  • 1
Zain
  • 95
  • 1
  • 8