VSCode java how do I set the path for the compiled .class files to go?

Viewed 4802

I'm using the Java Test Runner extension in VScode, 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 the sh*t out of this and either I have no idea what I'm saying or noone has ever had a problem with this before.

Thanks in advance for the help.

3 Answers

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.

enter image description here

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

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!

Related