在 Visual Studio Code IDE 和 OS X 中调试/运行标准 Java?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39582491/
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
Debug/run standard java in Visual Studio Code IDE and OS X?
提问by Greymarch
Love the light-weight Visual Studio Code in OS X. Have always wanted the ability to write, debug and run standard java (NOT javascript) from VSC in OS X. Found the following extension which allows you to debug and run java from VSC:
喜欢 OS X 中的轻量级 Visual Studio 代码。一直希望能够在 OS X 中从 VSC 编写、调试和运行标准 java(不是 javascript)。找到以下扩展,它允许您从 VSC 调试和运行 java:
https://marketplace.visualstudio.com/items?itemName=donjayamanne.javadebugger
https://marketplace.visualstudio.com/items?itemName=donjayamanne.javadebugger
Followed the directions as best as I could. Trying to run my java app, and I am getting a bunch of "error: cannot find symbol" errors in the debug-console window when I run my java app in VSC from OS X. App runs great in eclipse. What am I missing in either VSC or somewhere in OS X so I can properly run java code from VSC in OS X?
尽我所能遵循指示。尝试运行我的 java 应用程序,当我在 OS X 的 VSC 中运行我的 java 应用程序时,我在调试控制台窗口中收到一堆“错误:找不到符号”错误。应用程序在 eclipse 中运行良好。我在 VSC 或 OS X 中的某个地方缺少什么,以便我可以在 OS X 中从 VSC 正确运行 Java 代码?
回答by Jun Han
There is a much easier way to run Java, no configuration needed:
有一种更简单的方式来运行 Java,无需配置:
- Install the Code Runner Extension
- Open your Java code file in Text Editor, then use shortcut
Ctrl+Alt+N
, or pressF1
and then select/typeRun Code
, or right click the Text Editor and then clickRun Code
in context menu, the code will be compiled and run, and the output will be shown in the Output Window.
- 安装代码运行器扩展
- 在文本编辑器中打开您的 Java 代码文件,然后使用快捷方式
Ctrl+Alt+N
,或按下F1
然后选择/键入Run Code
,或右键单击文本编辑器,然后Run Code
在上下文菜单中单击,代码将被编译并运行,输出将显示在输出窗口。
回答by JerryGoyal
Code Runner Extension will only let you "run" java files.
Code Runner Extension 只会让你“运行”java 文件。
To truly debug 'Java' files follow the quick one-time setup:
要真正调试“Java”文件,请遵循快速一次性设置:
- Install Java Debugger Extensionin VS Code and reload.
- open an empty folder/project in VS code.
- create your java file (s).
- create a folder
.vscode
in the same folder. - create 2 files inside
.vscode
folder:tasks.json
andlaunch.json
- copy paste below config in
tasks.json
:
- 在 VS Code 中安装Java Debugger Extension并重新加载。
- 在 VS 代码中打开一个空文件夹/项目。
- 创建你的java文件(s)。
.vscode
在同一个文件夹中创建一个文件夹。- 在
.vscode
文件夹内创建 2 个文件:tasks.json
和launch.json
- 复制粘贴下面的配置
tasks.json
:
{ "version": "2.0.0", "type": "shell", "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" }, "isBackground": true, "tasks": [ { "taskName": "build", "args": ["-g", "${file}"], "command": "javac" } ] }
{ "version": "2.0.0", "type": "shell", "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" }, "isBackground": true, "tasks": [ { "taskName": "build", "args": ["-g", "${file}"], "command": "javac" } ] }
- copy paste below config in
launch.json
:
- 复制粘贴下面的配置
launch.json
:
{ "version": "0.2.0", "configurations": [ { "name": "Debug Java", "type": "java", "request": "launch", "externalConsole": true, //user input dosen't work if set it to false :( "stopOnEntry": true, "preLaunchTask": "build", // Runs the task created above before running this configuration "jdkPath": "${env:JAVA_HOME}/bin", // You need to set JAVA_HOME enviroment variable "cwd": "${workspaceRoot}", "startupClass": "${workspaceRoot}${file}", "sourcePath": ["${workspaceRoot}"], // Indicates where your source (.java) files are "classpath": ["${workspaceRoot}"], // Indicates the location of your .class files "options": [], // Additional options to pass to the java executable "args": [] // Command line arguments to pass to the startup class } ], "compounds": [] }
{ "version": "0.2.0", "configurations": [ { "name": "Debug Java", "type": "java", "request": "launch", "externalConsole": true, //user input dosen't work if set it to false :( "stopOnEntry": true, "preLaunchTask": "build", // Runs the task created above before running this configuration "jdkPath": "${env:JAVA_HOME}/bin", // You need to set JAVA_HOME enviroment variable "cwd": "${workspaceRoot}", "startupClass": "${workspaceRoot}${file}", "sourcePath": ["${workspaceRoot}"], // Indicates where your source (.java) files are "classpath": ["${workspaceRoot}"], // Indicates the location of your .class files "options": [], // Additional options to pass to the java executable "args": [] // Command line arguments to pass to the startup class } ], "compounds": [] }
You are all set to debug java files, open any java file and press F5(Debug->Start Debugging).
您已准备好调试 java 文件,打开任何 java 文件并按F5(调试->开始调试)。
Tip:*To hide .class files in the side explorer of VS code, open settings
of VS code and paste the below config:
提示:*要在 VS 代码的侧边资源管理器中隐藏.class 文件,请打开settings
VS 代码并粘贴以下配置:
"files.exclude": {
"*.class": true
}
回答by Ashwin
I can tell you for Windows.
我可以告诉你 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"); } }
从 VS Code Extensions 安装 Java Extension Pack 和 Code Runner Extension。
在 VS Code 设置中编辑您的 java 主页位置,“java.home”:“C:\Program Files\Java\jdk-9.0.4”。
检查是否在 VS Code 内部终端中识别了 javac。如果此检查失败,请尝试以管理员身份打开 VS Code。
在 Main.java 文件中创建一个简单的 Java 程序:
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.
右键单击 java 文件的任意位置并选择运行代码。
检查控制台中的输出。
Done, hope this helps.
完成,希望这会有所帮助。