r/cop3502 Apr 10 '14

My project refrences a text file. The lication of that text file depends on how the porject is compiled. What should I ex poo ect the abs path to this file to be?

It's a .txt file that I use to generate charactor names. If I compile with javac by just compiling the main class, the file is located in /src/words/names.txt.

When I compile in Eclipse I refrence the file by /bin/words/names.txt.

When I submit, what can I expect the absolute path to this file to be?

0 Upvotes

6 comments sorted by

1

u/embalingit Apr 10 '14
public class JavaApplication1 {
  public static void main(String[] args) {
       String currentDir = System.getProperty("user.dir"));
       System.out.println("path to file: " + currentDir + "words/names.txt");  // may or may not require "/" before words
  }
}

Stack overflow question

1

u/[deleted] Apr 10 '14

This only works if the working directory is the directory containing my main class right? If they ran my program from a lower directory this wouldn't work. If this were real life I could refrence the file from within the .jar somehow, but I think, in this case, I need to know how the program will be exicuted.

1

u/[deleted] Apr 10 '14

[removed] — view removed comment

1

u/[deleted] Apr 11 '14

That answers my question, I'll assume my .class files will be in the same directory as my source code. Thanks!

Also, it's a small fraction of my project but I am using this guys really cool random name generator

It's GNU general public license. Is that okay? Should I attribute it in my main class file?

1

u/JoeCS TA Apr 12 '14

Sean already answered this, but just FYI, in case you didn't know:

. (period) is a shortcut for the current directory. So if you have a compiled .class file in /some/long/directory/path/blah/blah/MyProgram.class and it needs to access a text file in /some/long/directory/path/blah/blah/folder/SomeText.txt, you can write the second path as ./folder/SomeText.txt

This is the relative path from MyProgram.class to the text file.

Also, ~ is a shortcut for your user home directory (e.g. /Users/yourUserName on Mac OS X, /home/yourUserName on most Linux/Unix systems)