简体   繁体   English

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

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

I would like to make a build system in Sublime Text 2 that will compile a Java file, and then run it in a new Terminal (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; 如果是这样,请逐步解释(我是Sublime Text 2的菜鸟),该怎么做; I've already tried posting on the Sublime Text 2 forums, and so far no luck! 我已经尝试在Sublime Text 2论坛上发帖,到目前为止还没有运气! I'd be inexpressibly grateful. 我无法形容地感激不尽。 Thanks for your time! 谢谢你的时间!

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. 完全impl是博客文章,而不是答案。
  • Assumes: OS X, xterm, no package hierarchy, etc. 假设:OS X,xterm,没有包层次结构等。
  • Package/project stuff is relatively straight-forward, but IMO awkward. 包裹/项目的东西相对简单,但IMO很尴尬。
  • I don't have a complete solution 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. 我的真实版本使用Ant或Maven,它解决了许多问题,但不是全部。
  • Some of this can be wrapped up in the sublime-build file, but… 其中一些可以包含在sublime-build文件中,但是......
  • …for me it's easier this way because of other stuff not shown here. ...对我而言,这种方式更容易,因为这里没有显示其他东西。

Nutshell (Simplification): compile and run through a shell script in order to get a new window. Nutshell (简化):编译并运行shell脚本以获得新窗口。

Script 脚本

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

JavaC.sublime-build JavaC.sublime建造

{
  "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; 所有这些说,我从来没有真正对Java适当的控制台输入做任何事情; 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. 我通过Groovy或JRuby REPL,或允许存根输入/输出源/目的地,或者......但不是Java,而不是Sublime Text 2 - 我使用IDE进行Java开发。 Anything else is a waste of my time, even for short, experimental stuff. 其他任何东西都浪费我的时间,即使是短期的实验性东西。

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. 有趣的是它还将文件编译为JAR。 Remove classes after compilation to make things neater and it also support generating JavaDocs. 编译后删除类以使事情更整洁,它也支持生成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. 您必须在终端中手动执行此操作。

This might help you but it only works for Linux at the moment, I am still working on a Windows version . 这可能会对你有所帮助,但它目前仅适用于Linux,我仍在使用Windows版本。 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. 我已经制作了一个bash脚本,用于在sublime text 2和3中运行java。这个脚本允许你使用包层次结构,但不是必需的。

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. 如果要传递参数,则需要使用package manage为sublime文本安装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. 您需要知道如何使用它的所有信息也可以在github上找到。 I hope this was helpful for you 我希望这对你有所帮助

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

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