}
javac -sourcepath src -d bin src/com/example/Main.java
java -cp bin com.example.Main
Stephan van Hulst wrote:Remember you have to restart the command prompt after you save environment variables.
I don't like to add a classpath to my environment variables though. I prefer to have a src directory in my project, and then run the java command with the -cp switch:
javac -sourcepath src -d bin src/com/example/Main.java
java -cp bin com.example.Main
}
Stephan van Hulst wrote:To explain my own example, when I run javac -sourcepath src -d bin src/com/example/Main.java (with the working directory being my project folder), I'm telling the compiler to compile src/com/example/Main.java, (which contains com.example.Main). If the compiler needs sources of classes referenced in Main.java, -sourcepath tells the compiler where it can find those sources. In this case, the src folder. The -d switch tells the compiler to put all the .class files in the bin folder, so I don't mix up my sources and compiled classes. So after compilation, com.example.Main will be in bin/com/example/Main.class.
To run the program, I tell java to execute com.example.Main. All it needs now is to know where the class file is, which I specify with the -cp switch. I tell it to find classes in the directory where I compiled them to: bin.
}
Stephan van Hulst wrote:Well, then all my code is in well organized places, and I don't have to mix up all sorts of project files with each other. I can put my program sources in a folder called src/main, test code in src/test, external libraries in lib, documentation in doc, output files in bin or target, settings in etc or conf. This makes it easy when you're looking for specific files.
It also means that I can actually have my projects completely separate from each other. Classes from different projects don't get mixed up in one big pile that you have on the classpath. You set the classpath specifically for the project you're currently working with using the -cp switch. This way, you can also have different classes that have the exact same name, for different projects.
}