0

Got a little problem loading a file! (Not a image just really a file like .txt and stuff) It loads fine in Netbeans with

    File myfile = new File("a/b/myfile.abc");

The problem is the compiled jar gets a exception and doesn't find the file. I need it as a file, not as a Stream or something, that's the problem and I have already tried everything that came into my mind to load it. I would like to load it externally (not from inside the jar) and the problem is it doesn't seem possible to get a working setup with getRessource(AsStream).

EDIT: Ok so i let it print the absolute path when it was compiled and when it was not compiled. Non compiled path:

C:\Users\USERNAME\Documents\NetBeansProjects\ProjectName\a\b\myfile.abc <-- Correct Path

Compiled path:

C:\Users\USERNAME\a\b\myfile.abc <-- Not Correct Path

Can anybody tell me how to fix this? EDIT²: If I navigate to the correct folder with cmd (cd etc.) and start the jar after doing so the folder is getting loaded from the correct directory. Can someone tell me what I need to change? EDIT³: When not starting with cmd it seems to search for the folder in Windows/system32 :O

Slashking
  • 1
  • 4
  • 1
    FYI, that filepath is relative to where your jar is, so make sure the jar and desired files are in the right place – foolmoron Nov 22 '13 at 22:22
  • Everything is in the right place I already checked that – Slashking Nov 22 '13 at 22:24
  • So, is that file a part of a jar or not? – dimoniy Nov 22 '13 at 22:35
  • What do you exactly mean? Its no class if thats what you are meaning. It also isnt inside the jars root (the file is inroot/myfile.abc and the jar is in root/blabla.jar) – Slashking Nov 22 '13 at 22:39
  • I mean is the file packaged inside the jar. Jar is basically just ZIP file. It can hold anything, not only compiled java code. I'm asking if the file you're trying to access is in the same jar as the class you're accessing it from? – dimoniy Nov 22 '13 at 22:45
  • "Everything is in the right place I already checked that". No the problem is that you do not understand. It is relative to the current working directory. – Robin Green Nov 22 '13 at 23:23
  • Could you explain what you mean? – Slashking Nov 22 '13 at 23:41

1 Answers1

0

When you construct a file with

new File("abc.txt")

the abc.txt file is supposed to be in the current directory. The current directory is the directory from which the java command is launched to execute your application. So, if you're in c:\foo\bar and execute java -cp d:\java\app\MyApplication.jar MyApplication, it will look for the file c:\foo\bar.

The location of the jar of the application is irrelevant, and doesn't have any impat on where the file is looked up. The current directory is what matters.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • But how can I tell java that the current directory is the "correct" directory (without starting cmd and doing it with the cd command)? – Slashking Nov 23 '13 at 13:54
  • How do you launch java, if not by starting a command line, going to a directory and typing the java command? If you start it by double-clicking a jar, then you shouldn't rely on relative paths. Use absolute paths instead. – JB Nizet Nov 23 '13 at 14:04
  • I want to start it by double clicking the jar and I also want relative paths as absolute paths are garbage when it comes to giving the jar to an other person. – Slashking Nov 23 '13 at 14:07
  • And how can you be sure that this other person will have a file a/b/myfile.abc? – JB Nizet Nov 23 '13 at 14:10
  • By giving him my whole dist directory? – Slashking Nov 23 '13 at 14:11
  • Then also give then a .cmd or .bat file to double-click on, and make sure that this shell script goes to the dist directory before launching the application. Or use an installer. But you shouldn't have modifiable files in the directory where the app is installed. You'd better store them where the user of the app wants to, or in the user's home directory. And if the file is not writable, then you should embed it in the jar itself. – JB Nizet Nov 23 '13 at 14:13
  • I think you just solved my problem... Give me a minute please – Slashking Nov 23 '13 at 14:14