Visual Studio Code - Java 类路径不完整。只会报告语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40831703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio Code - Java Classpath is incomplete. Only syntax errors will be reported
提问by rainer
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.
在对 python 进行了一些广泛的经验之后,我正在使用 java 迈出第一步。我正在运行的脚本是一个简单的 Java Swing Gui,它可以在命令行和 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.
为了设置 java 调试环境,我使用了 github 站点https://github.com/k--kato/vscode-javadebug上建议的 lauch.json 设置。
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....
我不知道问题是否来自 VS Code 内部,不知道是否是其他一些配置问题,例如 java 设置....
My working platform is Linux Ubuntu, Gnome Shell.
我的工作平台是 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:
这是我的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
}
]
}
回答by nightynvid
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.
搜索这个我发现 vscode 现在只识别 maven 项目或 eclipse 项目,所以它需要一个 .classpath 文件。所以最好的选择是先用maven创建项目,然后用vscode打开。
回答by Gorkem Ercan
This warning is displayed when you open a java file that the redhat.javaextension 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.projectfiles.
当您打开redhat.java扩展无法确定类路径的 java 文件时,会显示此警告。要获得扩展的全部好处,请使用带有 maven pom.xml 的项目(很快也会使用 gradle),或者使用最少的默认 eclipse 设置文件 aka .classpath .project文件。
回答by rainer
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.
由于 Microsoft Visual Studio Code 生态系统正在为 Java 快速发展,因此有一个方便的解决方案可以极大地帮助生成 - 只需几个步骤 - 可以与 VS Code 一起使用的正常运行的 Java 项目结构。
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.
当然,还有其他解决方案可以使用 Java,例如 NetBeans,但我一直非常喜欢 VS Code,只是等到出现更简单的东西时才重新使用它。
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.
我发现的非常简单的解决方案是使用 MAVEN。Maven 在您的主文件夹中创建整个项目结构和初始配置文件,您可以立即使用 VS Code 打开它并轻松运行它。
Of course, you will have to have the Java extensions installed, as explained here.
当然,你将必须安装有Java扩展,为解释在这里。
The solution I found hereon the net was a little outdated, so I have made some adaptations.
我找到了解决办法在这里在网上被一点点过时,所以我做了一些调整。
This is the process on a Linux machine:
这是Linux机器上的过程:
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
- In the top left of the debugger, on the right side of the green, where it says "no configuration", choose "Java" from the drop-down field and the .json file will be auto-generated.
Configure a Task.json
- Go to Task in the menu bar;
- Configure tasks ......
- Create task.json from template ....
- Select "maven" from the drop-down list and the .json file will be auto-generated.
检查你是否安装了 MAVEN
输入终端:
mvn --version
如果没有安装maven,输出会提示安装命令;
调用 maven quickstart 原型来生成你的新项目;
在终端类型或副本内:
mvn archetype:generate
这将打开一个可怕的列表,其中包含 2000 多种可供选择的 achetype。好消息是,如果你不填数字,maven-archetype-quickstart 会被自动选中,所以直接按回车。
从列表中选择一个版本并输入数字:我选择了建议的数字 = 6
为属性 groupId 选择一个值:
com.mycompany.app
为属性“artifactId”定义一个值(这将在您的主目录中创建文件夹):
my-app
定义“版本”的值:1.0
定义属性 'package' 的值:
com.mycompany.app
确认选项,将生成项目。
使用新项目启动 VS Code
在终端类型中:
code ./my-app
配置 Launch.json 文件
- 在调试器的左上角,绿色右侧,显示“无配置”的地方,从下拉字段中选择“Java”,将自动生成 .json 文件。
配置一个 Task.json
- 转到菜单栏中的任务;
- 配置任务......
- 从模板创建 task.json ....
- 从下拉列表中选择“maven”,将自动生成 .json 文件。
You are good to go.
你已准备好出发。
回答by Nobody Important
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.
我知道这是一个老问题,但任何在这里绊倒并需要快速轻松解决的人都可以在这里找到它。安装扩展:Eclipse New Java Project。
It emulates the behavior of the Eclipse action create Java Projectand should produce the results you need.
它模拟 Eclipse 操作create Java Project的行为,应该会产生您需要的结果。
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).
只需按下Ctrl + Shift + P
并输入 New Java 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!
它将创建所需的项目和文件,您不必担心该类路径错误。然后只需在src
新项目的文件夹中正常创建类文件并继续编程!
回答by Abdullah Khaled
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
我知道为时已晚,但是如果您mvn eclipse:eclipse
在根目录中使用此简单命令(假设您使用的是 maven)将项目转换为 eclipse,则可以简单地解决此问题,这将添加.classpath
文件。在这种情况下,您的src/*
文件将是项目文件
Tested on vscode and coc-java in vim too
也在 vim 中对 vscode 和 coc-java 进行了测试