简体   繁体   English

在 Visual Studio Code IDE 和 OS X 中调试/运行标准 Java?

[英]Debug/run standard java in Visual Studio Code IDE and OS X?

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.尝试运行我的 java 应用程序,当我在 OS X 的 VSC 中运行我的 java 应用程序时,我在调试控制台窗口中收到一堆“错误:找不到符号”错误。应用程序在 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?我在 VSC 或 OS X 中的某个地方缺少什么,以便我可以在 OS X 中从 VSC 正确运行 Java 代码?

There is a much easier way to run Java, no configuration needed:有一种更简单的方式来运行 Java,无需配置:

  1. Install the Code Runner Extension安装代码运行器扩展
  2. Open your Java code file in Text Editor, then use shortcut Ctrl+Alt+N , or press F1 and then select/type Run Code , or right click the Text Editor and then click Run 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 ,代码将被编译并运行,输出将显示在输出窗口中。

运行Jave

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 Extension in VS Code and reload.在 VS Code 中安装Java Debugger Extension并重新加载。
  • open an empty folder/project in VS code.在 VS 代码中打开一个空文件夹/项目。
  • create your java file (s).创建你的java文件(s)。
  • create a folder .vscode in the same folder.在同一文件夹中创建一个文件夹.vscode
  • create 2 files inside .vscode folder: tasks.json and launch.json.vscode文件夹中创建 2 个文件: tasks.jsonlaunch.json
  • copy paste below config in tasks.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" } ] }
  • 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": [] }

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 文件,请打开 VS 代码的settings并粘贴以下配置:

"files.exclude": {
        "*.class": true
    }

在此处输入图片说明

I can tell you for Windows.我可以告诉你 Windows。

  1. Install Java Extension Pack and Code Runner Extension from VS Code Extensions.从 VS Code Extensions 安装 Java Extension Pack 和 Code Runner Extension。

  2. Edit your java home location in VS Code settings, " java.home ": "C:\\\\Program Files\\\\Java\\\\jdk-9.0.4" .在 VS Code 设置中编辑您的 java 主页位置,“ java.home ”: "C:\\\\Program Files\\\\Java\\\\jdk-9.0.4"

  3. Check if javac is recognized in VS Code internal terminal.检查是否在 VS Code 内部终端中识别了 javac。 If this check fails, try opening VS Code as administrator.如果此检查失败,请尝试以管理员身份打开 VS Code。

  4. Create a simple Java program in Main.java file as:在 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.注意:不要在主类中添加包。

  1. Right click anywhere on the java file and select run code.右键单击 java 文件的任意位置并选择运行代码。

  2. 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.在扩展选项卡中,有 Microsoft 为 Visual Studio Code 发布的 Java 扩展包。 It installs 6 extensions for Java development (Language Support, Debugger, IntelliCode etc.).它为 Java 开发安装了 6 个扩展(语言支持、调试器、IntelliCode 等)。 I used it for the first time and found it quite easy to install.我第一次使用它,发现它很容易安装。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM