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

瘦欲@ 提交于 2019-11-29 04:23:17

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 relatively straight-forward, but IMO awkward.
  • 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.
  • 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.

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

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

{
  "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.

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

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.

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 . 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.

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

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!