Reputation: 3411
I am making my first steps with java, after some extensive experiences with python. The script I am running is a simple Java Swing Gui, that compiles and runs fine from the command line and within VS Code.
To set up the java debug environment, I used the lauch.json settings suggested on the github site https://github.com/k--kato/vscode-javadebug.
Unfortunately, every time I open the folder that contains the script, I get the following error message:
Warn: Classpath is incomplete. Only syntax errors will be reported.
I have no idea if the problem comes from within VS Code, of if it's some other configuration issue, such as the java set up....
My working platform is Linux Ubuntu, Gnome Shell.
Can anybody help?
This is the script:
//file name = SimpleEx.java
import java.awt.EventQueue;
import javax.swing.JFrame;
public class SimpleEx extends JFrame {
public SimpleEx() {
initUI();
}
private void initUI() {
setTitle("Simple example");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
SimpleEx ex = new SimpleEx();
ex.setVisible(true);
});
}
}
and this is my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Java",
"type": "java",
"request": "launch",
"stopOnEntry": true,
"cwd": "${fileDirname}",
"startupClass": "${fileBasename}",
"options": [
"-classpath",
"${fileDirname}"
]
},
{
"name": "Java Console App",
"type": "java",
"request": "launch",
"stopOnEntry": true,
"cwd": "${fileDirname}",
"startupClass": "${fileBasename}",
"options": [
"-classpath",
"${fileDirname}"
],
"externalConsole": true
}
]
}
Upvotes: 33
Views: 117560
Reputation: 4793
create .vscode/settings.json with the following content:
{
"java.project.sourcePaths": [
"<project_path>/src"
],
"java.project.referencedLibraries": [
"<libs_path>/**/*.jar"
]
}
Upvotes: 1
Reputation: 1
For the users facing this issue using visual studio code, you can try uninstalling the java extension you have been using and reinstall it.
Upvotes: 0
Reputation: 483
In VS Code just right-click on the src (Your Project Source Folder) and select the " Add folder to Java source path" option. If it didn't' worked, then try to remove it first by selecting "remove the folder from Java source path" (this will not result in any data loss) and then adding it again.
Restart once when done. Hope it works for you.
Upvotes: 15
Reputation: 539
I got this error along with Cannot resolve the modulepaths/classpaths automatically, please specify the value in the launch.json
during compilation.
All I had to do was open Command Palette
in View
or with Ctrl+Shift+P
and then run the command Clean Java Server Language Workspace
and everything started working normally again.
Upvotes: 9
Reputation: 121
I stumbled upon this issue and resolved it on my own. I was trying to compile and run my java file from outside of the folder it was inside. That's when I got the same "classpath" error op mentioned above. Once I moved into the directory in which the files were located, I was able to run without issue.
Upvotes: 1
Reputation: 51
Just press ctrl+shift+p and type Create Java Project and then click no build tools and it will ask to Select folder, then choose the folder and it ask for creating the file name, give the name to file It will create the project and files needed and you won't have to worry about that classpath error. Then just create your class files as normal in the src folder of your new project and proceed with your programming!
Upvotes: 1
Reputation: 11
In case this helps anyone else, I have to change this line in .classpath
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
to
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main">
I just had to change the path and it worked
Upvotes: 1
Reputation: 338
i know it's too late but you can simply solve this if you convert your project to eclipse with this simple command mvn eclipse:eclipse
(assuming you are using maven) at your root directory this will add .classpath
file. in this case your src/*
files will be project files
Tested on vscode and coc-java in vim too
Upvotes: 4
Reputation: 219
I know this is an old question, but anyone who stumbles here and needs a quick and easy fix may find it here. Install the extension: Eclipse New Java Project.
It emulates the behavior of the Eclipse action create Java Project and should produce the results you need.
Just press Ctrl + Shift + P
and type New Java Project (it will pop up after a few letters) and follow the simple directions. (it just asks for the name of the project).
It will create the project and files needed and you won't have to worry about that classpath error.
Then just create your class files as normal in the src
folder of your new project and proceed with your programming!
Upvotes: 21
Reputation: 3411
Since the Microsoft Visual Studio Code ecosystem is rapidly evolving for Java, there is a handy solution that enormously helps to generate - in just a few steps - a functioning Java project structure to use with VS Code.
There are, of course other solutions to work with Java, like NetBeans, but I have always liked VS Code a lot and just waited until something more easy came up to get back to using it.
The quite easy solution I found is using MAVEN. Maven creates the whole project structure and initial configuration files in your home folder and you can immediately open it with VS Code and run it without any hassle.
Of course, you will have to have the Java extensions installed, as explained here.
The solution I found here on the net was a little outdated, so I have made some adaptations.
This is the process on a Linux machine:
Check if you have MAVEN installed
Type into a terminal:
mvn --version
If maven is not installed, the output will suggest the installation command;
Invoke the maven quickstart archetype to generate your new project;
Inside the terminal type or copy:
mvn archetype:generate
This will open a scary list of over 2000 achetypes to choose from. The good news is that if you don't fill in a number, maven-archetype-quickstart will be automatically selected, so just press ENTER.
Choose a version from the list and type in the number: I picked the suggested number = 6
Choose a value for property groupId:
com.mycompany.app
Define a value for property 'artifactId' (this will create the folder in your home directory) :
my-app
Define value for 'version' : 1.0
Define value for property 'package' :
com.mycompany.app
Confirm the options and the project will be generated.
Start VS Code with the new project
In the terminal type :
code ./my-app
Configure a Launch.json file
Configure a Task.json
You are good to go.
Upvotes: 12
Reputation: 3226
This warning is displayed when you open a java file that the redhat.java extension can not determine a classpath for. To get the full benefits of the extension use either a project with maven pom.xml (soon also gradle), or least default eclipse setting files aka .classpath .project files.
Upvotes: 11
Reputation: 76
searching for this i found that vscode right now only recognizes maven projects, or eclipse projects so it needs a .classpath file. So the best option is create the project with maven first and then open with vscode.
Upvotes: 6