Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 996
Matthew Bennett Avatar asked Aug 25 '20 17:08

Matthew Bennett


People also ask

How do I add Java build path to VS Code?

The classpath can be set manually in the classpath configuration page. You can open it by executing the Java: Configure Classpath command from the Command Palette (Ctrl+Shift+P).

How do I change the path in a VS Code?

Windows and Linux installations should add the VS Code binaries location to your system path. If this isn't the case, you can manually add the location to the Path environment variable ( $PATH on Linux). For example, on Windows, VS Code is installed under AppData\Local\Programs\Microsoft VS Code\bin .

Where is .class file stored?

1. class file in java is generated when you compile . java file using any Java compiler like Sun's javac which comes along with JDK installation and can be found in JAVA_HOME/bin directory.


1 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

like image 83
Tony Chan Avatar answered Oct 25 '22 01:10

Tony Chan