Hi everyone,
I am a complete newbie to java and I'm getting overwhelmed with the barrier of entry to even start writing code. I have VSCode and have my java file, but I can't figure out how to run the code so I can see Hello World!
Here is the code:
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" in the terminal window.
System.out.println("Hello, World");
}
}
And here is the error message when I do java HelloWorld :
Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
What do I do? I thought I downloaded all the right extensions and everything. Why can't I just write the code and click a button to run it?
Thank youcompilation - How to compile and run Java code in Visual Studio Code - Stack Overflow
Debug/run standard java in Visual Studio Code IDE and OS X? - Stack Overflow
visual studio code - Running a java program in VScode - Stack Overflow
How to run Java in VScode
Videos
As a first step, try to compile your programm from te command line. E.g. How do I run a Java program from the command line on Windows? is a good start. You can run the commands directly in VSCode's integrated terminal. Optionally, create a VS Code build task from that command to run builds with the build command.
{
"version": "2.0.0",
"tasks": [
{
"label": "Build Foo",
"type": "shell",
"command": "javac foo.java",
"problemMatcher": []
}
]
}
To run/debug, create a launch config in the debug viewlet, for example:
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug (Launch)-Foo",
"request": "launch",
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopOnEntry": false,
"mainClass": "Foo",
"args": ""
},
{
"type": "java",
"name": "Debug (Attach)",
"request": "attach",
"hostName": "localhost",
"port": 0
}
]
}
If you are using Maven, you should see a MAVEN tab at the bottom of the left menu bar. Clicking on it will expand to show your project name. Right click on it shows a "Run Maven Commands" option, under which you will see Compile and other stuffs like run and debug.
I can tell you for Windows.
Install Java Extension Pack and Code Runner Extension from VS Code Extensions.
Edit your java home location in VS Code settings, "
java.home":"C:\\Program Files\\Java\\jdk-9.0.4".Check if javac is recognized in VS Code internal terminal. If this check fails, try opening VS Code as administrator.
Create a simple Java program in Main.java file as:
public class Main {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Note: Do not add package in your main class.
Right click anywhere on the java file and select run code.
Check the output in the console.
Done, hope this helps.
In the extensions tab, there is Java Extension Pack published by Microsoft for Visual Studio Code. It installs 6 extensions for Java development (Language Support, Debugger, IntelliCode etc.). I used it for the first time and found it quite easy to install.
If your code needn't input data, you can add this in the launch.json:
"console": "internalConsole"
The default value is:
"console": "integratedTerminal"
I do not suggest install the 'Code-Runner' extension, because it will compile the java file under the same folder of the java file, and mix them up.
And I recommended you to get used to the outputs, the outputs can provide useful information, let you know what's exactly the vscode does. If you run into some problems, you will need this information to help you to solve the problem.
In this case, you can just press Command F5 (Mac) or other keybindings (https://code.visualstudio.com/docs/getstarted/keybindings). This runs the program in your IDE. Otherwise, you can create a launch.json file to configure your debugging.