1

I have a small java project. And i want to keep a structure in how the files are organized in my project folder.

There are 3 folders: bin/, res/ and src/. In src are all source files. In res are some resources like textfiles to be printed are files that are saved during runtime. And in bin is where all the binarys should be.

I found out, that i can compile my sourcefiles with the -d argument to the bin folder.

javac -d bin src/*.java

But I can't run java bin/Main from my project folder it gives me a classnotfound error. (Going in bin and then run java Main works. why??)

Second problem is, that res files are only accessible when res/ is in bin/. I want it kind of like in Eclipse. In the sourcecode files in res/ are used like the executor is in the project folder.

I hope you understand what i'm trying to do. And thanks for any help!

a2r
  • 45
  • 5

1 Answers1

3

Java requires the .class files to be locatable from the classpath root directory. So if your classpath is ., then the class com.mycompany.MyClass should be at ./com/mycompany/MyClass.class. So to run your files from your base directory, set the classpath by giving the -cp bin command-line argument to java.

How are you trying to get to the res files? If you use the -cp trick above, you should be fine with something like new FileReader("res/file1.txt"). If you run in the bin folder, you would need new FileReader("../res/file1.txt")

Keith Randall
  • 22,985
  • 2
  • 35
  • 54
  • Thank you that worked. Yeah I'm accessing them like you mentioned. In the man pages there was something about sourcepath and classpath. I was a little confused because the -d parameter compiles my files into another folder. But what does classpath do then? ( and sourcepath??) – a2r Oct 09 '12 at 23:08