Matthew Bennett
Matthew Bennett

Reputation: 91

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

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

Answers (3)

Harender Singh
Harender Singh

Reputation: 1

  1. Open VS Code Settings (Ctrl + ,).
  2. Search for "Code Runner: Executor Map".
  3. Click "Edit in settings.json".
  4. insert following line inside "code-runner.executorMap": { insert here } and save it.

"java": "javac -d custom_folder_name $fullFileName && java -cp custom_folder_name $fileNameWithoutExt"

  1. Comment out or delete old "java" : ............ value
  2. Rename the custom_folder_name as you like or insert the full path to the folder where you want to store .class files.
  3. Don't delete or modify other languages running command if they appear automatically.

Explanation:

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

Pranjal Kumar
Pranjal Kumar

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

Tony Chan
Tony Chan

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.

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

Upvotes: 4

Related Questions