Reputation: 91
I'm using the Java Test Runner extension in VS code, and I want it to compile my classes to a specific folder. I found the setting to change the -cp when actually running the compiled .class files, but the whole thing still fails, since the extension builds the .class files in an obscure temporary folder deep in it's appdata folder.
I would like it to compile my .java classes into a folder I have within my project, so that I can have them there. I have tried googling it and, either I have no idea what I'm typing, or no one has ever had a problem with this before.
Thanks in advance for the help.
Upvotes: 9
Views: 13976
Reputation: 1
"java": "javac -d custom_folder_name $fullFileName && java -cp custom_folder_name $fileNameWithoutExt"
"java" : ............
valueExplanation:
javac -d custom_folder_name $fullFileName
→ Stores .class file inside the custom_folder_name folder after compiling.
java -cp custom_folder_name $fileNameWithoutExt
→ Runs Java program from the custom_folder_name folder.
Upvotes: 0
Reputation: 1
First of all, you can create a CLASSPATH location in your environment variable section, after you set your path location.
Also, what people usually mistake is that they install a code runner used to compile and run all the languages, so if you are running your java code through the top-right run button, then click on the drop down menu beside it and click the "run java" button and I guess then your code will run just fine.(if you have downloaded java runner extension separately)
I hope this helps!
Upvotes: 0
Reputation: 8097
This is a late answer, but I just ran into this so I hope it helps others.
As of Feb 4, 2021 there is now a setting for the Java Language Support for VS Code extension, where you can specify the output directory for compiled .class files. If you're using VS Code to run Java I'm almost certain you'd have this extension installed.
Simply open your VS Code settings and search for java.project.outputPath
. As you'll notice, it states this setting only applies for Workspace settings, not User settings. So make sure to switch to the Workspace tab before clicking Edit in settings.json
. This will edit the settings.json
file in your projects .vscode
folder.
Also note this doesn't work for projects managed with Maven/Gradle. It's great for simple projects though.
For the curious: link to the GitHub pull request where the feature was added to the extension (also has a neat gif of feature in action): https://github.com/redhat-developer/vscode-java/pull/1694
Upvotes: 4