Sublime Text 2 构建系统以在新的终端/命令提示符窗口中编译和运行 Java?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12291058/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:19:36  来源:igfitidea点击:

Sublime Text 2 build system to compile & run Java in a new Terminal/Command Prompt window?

javaterminalcommand-promptsublimetext2build-system

提问by Dave Newton

I would like to make a build system in Sublime Text 2 that will compile a Java file, and then run it in a newTerminal (for OS X or Linux) or Command Prompt (for Windows) window.

我想在 Sublime Text 2 中创建一个构建系统来编译一个 Java 文件,然后在一个新的终端(对于 OS X 或 Linux)或命令提示符(对于 Windows)窗口中运行它。

The reason for this is because Sublime Text 2 doesn't allow users to input anything, so any programs requiring input will spit out an error when running inside Sublime Text 2, like this:

这样做的原因是因为 Sublime Text 2 不允许用户输入任何内容,因此任何需要输入的程序在 Sublime Text 2 中运行时都会吐出错误,如下所示:

This is what I currently have (I've also tried a batch file), but it simply runs inside Sublime Text 2, as opposed to in a new shell:

这是我目前拥有的(我也试过一个批处理文件),但它只是在 Sublime Text 2 中运行,而不是在新的 shell 中运行:

Is this possible? If so, please explain, step-by-step (I'm a noob at Sublime Text 2), how to do it; I've already tried posting on the Sublime Text 2 forums, and so far no luck! I'd be inexpressibly grateful. Thanks for your time!

这可能吗?如果是这样,请逐步解释(我是 Sublime Text 2 的菜鸟),如何做;我已经尝试在 Sublime Text 2 论坛上发帖,但到目前为止没有运气!我会说不出的感激。谢谢你的时间!

回答by Dave Newton

Here's the "polite" (read: short and readable) version of what I did to make this work.

这是我为完成这项工作所做的工作的“礼貌”(阅读:简短易读)版本。

  • This is a starting point only. Full impl is a blog post, not an answer.
  • Assumes: OS X, xterm, no package hierarchy, etc.
  • Package/project stuff is relativelystraight-forward, but IMO awkward.
  • I don't have a completesolution that's cross-OS or that takes weird directories into account.
  • My real version makes some assumptions that may or may not work for the rest of the world.
  • My real version uses Ant or Maven, which solves many problems, but not all.
  • Some of this can be wrapped up in the sublime-build file, but…
  • …for me it's easier this way because of other stuff not shown here.
  • 这只是一个起点。Full impl 是一篇博客文章,而不是一个答案。
  • 假设:OS X、xterm、无包层次结构等。
  • 包/项目的东西相对简单,但 IMO 很尴尬。
  • 我没有跨操作系统或考虑奇怪目录的完整解决方案。
  • 我的真实版本做了一些假设,这些假设可能适用于世界其他地方,也可能不适用。
  • 我的真实版本使用Ant或Maven,解决了很多问题,但不是全部。
  • 其中一些可以包含在 sublime-build 文件中,但是……
  • ……对我来说,这种方式更容易,因为这里没有显示其他内容。

Nutshell(Simplification): compile and run through a shell script in order to get a new window.

Nutshell(简化):编译并运行shell脚本以获得一个新窗口。

Script

脚本

cd 
/usr/bin/javac 
/usr/X11/bin/xterm -e "/bin/bash -c \"/usr/bin/java ; echo 'Press ENTER to quit...'; read line\""

JavaC.sublime-build

JavaC.sublime-build

{
  "cmd": ["~/bin/run-java.sh $file_path $file $file_base_name"],
  "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
  "path": "/usr/bin/java",
  "selector": "source.java",
  "shell": true
}

In real life it's a bit more complex.

在现实生活中,情况要复杂一些。

All this said, I never really do anything with console input in Java proper; I do it via either a Groovy or JRuby REPL, or allow stubbing of input/output sources/destinations, or… but not in Java, and not from Sublime Text 2–I use an IDE for Java development. Anything else is a waste of my time, even for short, experimental stuff.

综上所述,我从来没有真正对 Java 中的控制台输入做任何事情;我通过 Groovy 或 JRuby REPL 来实现,或者允许存根输入/输出源/目标,或者……但不是在 Java 中,也不是来自 Sublime Text 2——我使用 IDE 进行 Java 开发。其他任何事情都是浪费我的时间,即使是简短的实验性内容。

回答by Tushortz

I didn't have to use all these long methods. Well not for big projects. An example build system is

我不必使用所有这些冗长的方法。不适合大项目。一个示例构建系统是

{
    "cmd": ["javac '$realpath$file' && java $file_base_name && rm *.class"],
    "selector": "source.java",
    "shell": true,
    "variants": [

        {
            "name": "JavaDoc",
            "cmd": ["mkdir documentation && javadoc -d documentation *.java"]
        },

        {
            "name": "JAR",
            "cmd": ["javac '$realpath$file' && echo \"Main-Class: $file_base_name\" > Manifest.txt && jar cfm $file_base_name.jar Manifest.txt *.class && rm *.class && java -jar $file_base_name.jar"]
        },


    ]
}

This works for me on Linux and can be downloaded on Github at Java,sublime-build

这在 Linux 上对我有用,可以在 Github 上的Java,sublime-build 上下载

The interesting thing is that it also compile files to JAR. Remove classes after compilation to make things neater and it also support generating JavaDocs.

有趣的是,它还可以将文件编译为 JAR。编译后删除类使事情更整洁,它还支持生成JavaDocs。

The only limitation is that it cannot accept user input or arguments at compile time. You would have to do that manually in the terminal.

唯一的限制是它在编译时不能接受用户输入或参数。您必须在终端中手动执行此操作。

回答by Danny van Tol

This might help you but it only works for Linux at the moment, I am still working on a Windows version . I've made a bash script for running java in sublime text 2 and 3. This script allows you to use package hierarchy but is not required.

这可能对您有所帮助,但目前仅适用于 Linux,我仍在开发 Windows 版本。我已经制作了一个用于在 sublime text 2 和 3 中运行 java 的 bash 脚本。该脚本允许您使用包层次结构,但不是必需的。

It can be downloaded on github using this link: https://github.com/dannyvantol/JPack

可以使用以下链接在 github 上下载:https: //github.com/dannyvantol/JPack

If you want to pass argument you need to install GLUE for sublime text using package manage.

如果你想传递参数,你需要使用包管理为 sublime text 安装 GLUE。

I don't know if the linux version works on windows with Cygwin install, But you can try it.

我不知道 linux 版本是否适用于安装了 Cygwin 的 Windows,但您可以尝试一下。

All the informatie you need to know how to use it can also be found on github. I hope this was helpful for you

您需要知道如何使用它的所有信息也可以在 github 上找到。我希望这对你有帮助