0

I want to compile and run a Java program in the terminal through Sublime Text 3 instead of using an IDE like Eclipse. I have edited the Java.sublime-build file to do this, but it is not working. When I launch the build system, gnome-terminal opens for a while and then crashes.

Here is the .sublime-build I edited:

{
"cmd": ["gnome-terminal -x bash -c\"javac ${file_name} && java ${file_base_name}; exec bash\""],
"selector": "source.java",
"shell":true,
"working_dir": "$file_path"
}

What other changes do I have to make to get this to compile Java programs for me?

sentientmachine
  • 347
  • 3
  • 14
  • What do you mean by "crash"? Do you get any output? Maybe adding a "sleep 60" or the like helps so you can see output before the shell terminates. – Clemens Klein-Robbenhaar Mar 06 '15 at 19:35
  • @Clemens Klein-Robbenhaar I'm sorry, I don't know if it's a real crash or a normal way of closing. It opens and then closes practically at the same moment. – sentientmachine Mar 06 '15 at 20:35
  • That is why I suggest to add a "sleep 60" to keep he window open for another 60 seconds, like `-c\"javac ${file_name} && java ${file_base_name}; sleep 60\"` – Clemens Klein-Robbenhaar Mar 06 '15 at 20:49
  • Tried with your method, but doesn't work, same problem. – sentientmachine Mar 08 '15 at 09:57
  • The `sleep 60` should not solve the problem. All it should do is to keep the terminal window open for 60 seconds so you can see the error message. Maybe `-c\"javac ${file_name} && java ${file_base_name} || sleep 60; exec bash\"` works better (though I have no idea what the `exec bash` really does there ...) – Clemens Klein-Robbenhaar Mar 08 '15 at 11:52
  • something completely different: likely the problem is that some dependencies are missing; in that case wrapping everything in an executable jar (in Eclipse) and then executing the executable jar via `java -jar jarfilename.jar` might work better. See second answer to http://stackoverflow.com/questions/5258159/how-to-make-an-executable-jar-file – Clemens Klein-Robbenhaar Mar 08 '15 at 12:16
  • Really worked. Now I only have to change the ST3 build system. – sentientmachine Mar 08 '15 at 13:45

1 Answers1

0

Try this.

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

tushortz
  • 3,697
  • 2
  • 18
  • 31