When we compile a java code the .class file is created in the same place where the source code is . Is there a way like when we compile a java code the .class file file will be formed in a different folder
I tried creating projects and then writing the code but it didn't work
CodePudding user response:
Yes there is. When you compile your java file with
javac file.java
This default stores .class of that file in same folder.
To store .class in different folder use -d paramter with javac as:
javac file.java -d "Path"
For ex:
javac file.java -d "D:/Classes"
Then while running the files you have to set classpath so that JVM will search class files in that folder:
java -classpath "D:/Classes" file
FYI: If your file name is say Test.java and when you run javac, Test.class gets created at the path mentioned.
CodePudding user response:
You can change the path where your .class is created following the next steps:
- File -> Preferences ->Settings
- Write java.output on the search bar and it should show you "Java > Project : Output Path.
- Click on edit in setting.json and write the new path where you want your .class to be generated.
CodePudding user response:
Do you compile manually with command line javac ? If so you can make a bash script (if you are on linux) that move all class file to another directory after compilation.
Something like:
mv *.class destination/
I think you have similar command on windows.
In vscode you can can then configure your launch.json to execute that command as well as the compilation one.