如何在 Mac OS 上使用 Sublime Text 3 运行 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24319143/
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
How to run Java using Sublime Text 3 on Mac OS
提问by styner
Recently, I have got to know Sublime Text 3, and tried to run Java in it.
最近,我开始了解 Sublime Text 3,并尝试在其中运行 Java。
I have modified JavaC.sublime-build in the package, Java.sublime-package.
我在包 Java.sublime-package 中修改了 JavaC.sublime-build。
JavaC.sublime-build:
JavaC.sublime-build:
{
"cmd": ["runJava.sh", "$file_base_name"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
I have shell script(runJava.sh) as below, and I put it in the Java bin folder.
我有如下的 shell 脚本(runJava.sh),我把它放在 Java bin 文件夹中。
runJava.sh:
运行Java.sh:
[ -f ".class" ] && rm .class
for file in .java
do
echo "Compiling $file........"
javac $file
done
if [ -f ".class" ]
then
echo "-----------OUTPUT-----------"
java
else
echo " "
fi
Java bin folder:
Java bin 文件夹:
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin
However, when I build a java file, I got an error message saying that....
但是,当我构建一个 java 文件时,我收到一条错误消息说....
[Errno 2] No such file or directory: 'runJava.sh'
[cmd: ['runJava.sh', 'Test']]
[dir: /Users/xxxxxxx/Desktop]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
[Finished]
I have no idea why this message showed up, because runJava.sh is located in the right place, which is the Java bin folder.
我不知道为什么会出现此消息,因为 runJava.sh 位于正确的位置,即 Java bin 文件夹。
What is the reason of this problem?
这个问题的原因是什么?
回答by MattDMo
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin
is not in Sublime's path, as indicated by your error message. There are two ways to get around this. The first is to either move runJava.sh
to /usr/bin
, or create a symlink to it there. The second is to alter your JavaC.sublime-build
file like so:
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin
不在 Sublime 的路径中,如您的错误消息所示。有两种方法可以解决这个问题。第一个是移动runJava.sh
到/usr/bin
,或者在那里创建一个符号链接。第二个是JavaC.sublime-build
像这样改变你的文件:
{
"cmd": ["runJava.sh", "$file_base_name"],
"path": "/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin:$PATH",
"shell": true,
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
This adds your Java bin
folder to Sublime's PATH
when the build system is executing, so that it can find runJava.sh
. Adding "shell": true
runs the command through bash, which will allow it to pick up your other environment variables.
这会在构建系统执行时将您的 Javabin
文件夹添加到 Sublime 的文件夹中PATH
,以便它可以找到runJava.sh
. 添加"shell": true
通过 bash 运行命令,这将允许它获取您的其他环境变量。
回答by Ynty Wanady
I modified mine to be as this
我将我的修改为这样
to run you do ctrl + shift + b
运行你做 ctrl + shift + b
{
"shell_cmd": "javac \"$file\"",
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java",
"variants":
[
{
"name": "Run",
"shell_cmd": "java $file_base_name"
}
]
}
回答by Alex
That's because you are not supposed to modify package files directly with an editor. They are zip files, despite they don't have an extension to warn the user.
那是因为您不应该直接使用编辑器修改包文件。它们是 zip 文件,尽管它们没有警告用户的扩展名。
To modify the plug-in you have to go to /Packages and unzip the Java.sublime-package file. I'll use linux syntax but the procedure remains the same:
要修改插件,您必须转到 /Packages 并解压缩 Java.sublime-package 文件。我将使用 linux 语法,但过程保持不变:
cd <sublime-text3-folder>/Packages
mkdir java
cp Java.sublime-packages java
cd java
unzip Java.sublime-packages
Then use an editor to modify JavaC.sublime-build to add the following lines (don' forget the extra comma after the last line
然后用编辑器修改 JavaC.sublime-build 添加以下几行(别忘了最后一行后的多余逗号
{
"shell_cmd": "javac \"$file\"",
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java",
"variants":
[
{
"name": "Run",
"shell_cmd": "java $file_base_name"
}
]
}
Zip again the contents in Java.sublime-package and put it back in Package folder via:
再次压缩 Java.sublime-package 中的内容,并通过以下方式将其放回 Package 文件夹中:
zip Java.sublime-package * cp Java.sublime-package ..//Packages
zip Java.sublime-package * cp Java.sublime-package ..//包
Restart sublime and now along with Ctrl+B to build your project you will be able to run it with Ctrl + Mayus + B
重新启动 sublime,现在与 Ctrl+B 一起构建您的项目,您将能够使用 Ctrl + Mayus + B 运行它
回答by BenS
There are many solutions out there that kind of say the same thing without providing a step-by-step. So here was my experience on a Mac'16 with Sublime Text 3:
有很多解决方案都在说同样的事情,而没有提供逐步的说明。所以这是我在 Mac'16 上使用Sublime Text 3 的体验:
Open a terminal window and do the following:
打开终端窗口并执行以下操作:
$ cd /Applications/Sublime\ Text.app/Contents/MacOS/Packages/
$ cd /Applications/Sublime\ Text.app/Contents/MacOS/Packages/
Made a temp directory to mess with.
制作了一个临时目录来处理。
$ mkdir java
$ mkdir java
Copy current Java.sublime-packageinto new javadirectory and cdinto java.
将当前的Java.sublime-package复制到新的java目录中,然后将cd复制到java 中。
$ cp Java.sublime-package java/
$ cd java
$ cp Java.sublime-package java/
$ cd java
Then, unzip the package to see the contents:
然后,解压缩包以查看内容:
$ unzip Java.sublime-package
$ 解压 Java.sublime-package
Now, edit the build file, JavaC.sublime-build. If you have sublime text's command line script:
现在,编辑构建文件JavaC.sublime-build。如果你有 sublime text 的命令行脚本:
$ subl JavaC.sublime-build
$ subl JavaC.sublime-build
Otherwise,
除此以外,
$ vi JavaC.sublime-build
$ vi JavaC.sublime-build
Then I copied the following into my JavaC.sublime-buildwhich I found here posted by Sean Mullen:
然后我将以下内容复制到我的JavaC.sublime-build 中,我在这里找到了Sean Mullen发布的内容:
{
"cmd": ["javac \"$file_name\" && java \"$file_base_name\""],
"shell": true,
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
After that, save, and while inside the javadirectory type:
之后,保存,并在java目录中键入:
$ zip Java.sublime-package *
$ zip Java.sublime-package *
Move new build package to necessary folder (parent directory):
将新的构建包移动到必要的文件夹(父目录):
$ mv Java.sublime-package ../
$ mv Java.sublime-package ../
UPDATE: 4/18/18: Sorry it took me so long to adjust this answer. Based on comments from @bumbu, we do need to execute the following command before removing the temp directory:
更新:2018 年 4 月 18 日:对不起,我花了这么长时间来调整这个答案。根据@bumbu 的评论,我们确实需要在删除临时目录之前执行以下命令:
$ cd ..
$ cd ..
Remove temp javadirectory:
删除临时java目录:
$ rm -fr java/
$ rm -fr java/
And DONE. Now building the Java file will now attempt to run it as well. I prefer this behavior instead of having to do another "variant" to run the program.
并做了。现在构建 Java 文件也将尝试运行它。我更喜欢这种行为,而不必执行另一个“变体”来运行程序。
回答by steel
Easy step-by-step:
简单的一步一步:
Install Package Controlif you have't already
Using Package Control, install Package Resource Viewer
Open Package Control, type
prv
and runPackageResourceViewer: Open Resource
Select
Java
Select
JavaC.sublime-build
Replace the build JSON definition with the below code block exactly
You're done!
ctrl+b
should now compile and run your Java file.
如果您还没有安装包控制
使用包控制,安装包资源查看器
打开包控制,输入
prv
并运行PackageResourceViewer: Open Resource
选择
Java
选择
JavaC.sublime-build
完全用下面的代码块替换构建 JSON 定义
你完成了!
ctrl+b
现在应该编译并运行您的 Java 文件。
Build JSON definition:
构建 JSON 定义:
{
"cmd": ["javac \"$file_name\" && java \"$file_base_name\""],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java",
"shell": true
}
回答by Feng Yi
In Sublime Text 3, you just need simply click tools -> Build System -> New Build Systemand then paste the following from Sean Mullen:
在 Sublime Text 3 中,您只需单击工具 -> 构建系统 -> 新构建系统,然后从Sean Mullen粘贴以下内容:
{
"cmd": ["javac \"$file_name\" && java \"$file_base_name\""],
"shell": true,
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
Then Save it as java_run.sublime-build
然后另存为java_run.sublime-build
That's it! When you click tools -> Build Systemagain, you'll see java_run, choose this and then try to build, the java file will be compiled and run automatically.
就是这样!再次点击tools -> Build System,你会看到java_run,选择这个然后尝试build,java文件会自动编译运行。