简体   繁体   English

在 Windows 的 Sublime Text 上运行 Java

[英]Running Java on Sublime Text for Windows

I'm trying to compile and execute Java files on Sublime text.我正在尝试在 Sublime 文本上编译和执行 Java 文件。 The JavaC build system does not work, which I'm assuming is normal because you're supposed to create a file with the build system code and save it in "Packages". JavaC 构建系统不起作用,我假设这是正常的,因为您应该使用构建系统代码创建一个文件并将其保存在“包”中。 I know this because I set it up for Python a while ago, and I've been using it normally.我知道这一点是因为我不久前为 Python 设置了它,并且我一直在正常使用它。

I tried to do the same for Java, using the same build system code but changing the python parts to java.我尝试对 Java 做同样的事情,使用相同的构建系统代码,但将 python 部分更改为 java。 When using the build system I get an error saying that 'javac' is not recognized as an internal or external command,operable program or batch file.使用构建系统时,我收到一条错误消息,指出'javac' is not recognized as an internal or external command,operable program or batch file.

For reference, here's what I have作为参考,这是我所拥有的

{
  "cmd": ["javac", "$file", "&&", "java", "$file_base_name"],
  "shell": true,
  "path":"C:\\Program Files\\Java\\jdk-11.0.2\\bin\\java.exe",
  "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
  "selector": "source.java"
}

I'm trying to run a simple Hello World program.我正在尝试运行一个简单的 Hello World 程序。 This is on Windows 10, and the file name is saved with the .sublime-build extension.这是在 Windows 10 上,文件名以 .sublime-build 扩展名保存。 What am I doing wrong?我究竟做错了什么?

The error message you're seeing indicates that when Sublime tells Windows to execute javac , Windows can't find it.您看到的错误消息表明,当 Sublime 告诉 Windows 执行javac ,Windows 找不到它。 The general reason for that is that the PATH environment variable doesn't contain the location where javac.exe is located.造成这种情况的一般原因是PATH环境变量不包含javac.exe所在的位置。

In this case you've used the path key in the sublime-build file to explicitly set the path, but you're setting it to the name of the executable;在这种情况下,您已使用sublime-build文件中的path键显式设置路径,但您将其设置为可执行文件的名称; path should contain the location where the executable can be found and not point to the executable itself. path应该包含可执行文件所在的位置,而不是指向可执行文件本身。

You can fix this in one of two ways:您可以通过以下两种方式之一解决此问题:

  1. Remove the path line and change the first item in cmd so it it includes the full path up to and including the javac.exe file;删除path行并更改cmd的第一项,使其包含直到并包括javac.exe文件的完整路径; then Sublime will tell Windows to execute the file directly without having to look it up in the path.然后 Sublime 会告诉 Windows 直接执行文件,而不必在路径中查找它。

  2. Modify the path line to remove the javac.exe part;修改path行去掉javac.exe部分; then Sublime will tell windows to execute javac.exe and it will be able to find it in the directory indicated in the path.然后 Sublime 会告诉 windows 执行javac.exe ,它就能在路径中指定的目录中找到它。

More information on build systems (including problems similar to this one) can be found in the video Common Build Problems ( Disclaimer: I am the author of the video)有关构建系统的更多信息(包括与此类似的问题)可以在视频常见构建问题中找到免责声明:我是视频的作者)

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

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