Skip to content

Instantly share code, notes, and snippets.

@DevinClark
Created January 18, 2012 16:18
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save DevinClark/1633819 to your computer and use it in GitHub Desktop.
Save DevinClark/1633819 to your computer and use it in GitHub Desktop.
This is a build script for Sublime Text 2 that will compile and run the open java file by simply pressing cmd + B. I am very new at Java so feel free to point out problems with this script. You can just drop this file in the User Packges folder and restar
{
"cmd": ["javac", "$file_name"],
"cmd": ["java", "$file_base_name"],
"working_dir": "${project_path:${folder}}",
"selector": "source.java"
}
@dblandin
Copy link

It seems that the Sublime Build System only allows for one cmd. The most common solution I've found is to make a bash script with multiple commands and run the bash script from the Sublime build system.

#!/bin/bash          
# compiles all java files within directory and runs first argument
for file in *.java
do
echo "Compiling $file"
javac $file
done
echo "Running $1"
java $1
{
    "cmd": ["build_java.sh", "$file_base_name"]
}

I placed my build_java.sh script within the /usr/bin directory

@dikamilo
Copy link

{
    "cmd": ["javac", "$file_name", "&&", "java", "$file_base_name"],
    "working_dir": "${project_path:${folder}}",
    "selector": "source.java",
    "shell": true
}

@danpe91
Copy link

danpe91 commented Aug 22, 2012

hey, what if I want to import some external jars?

@vgaldikas
Copy link

I just did it very similar to what dblandin showed, actually its identical to what he did except for that bash script:

#!/bin/bash
# compiles all java files within directory and runs first argument
for file in *.java
 do
  echo "Compiling $file"
  javac $file
 done

gnome-terminal -e "bash -c \"java $1; echo 'Press ENTER to quit...'; read line\$

The running of program in Sublime's console was buggy.. I tried to mess around with it.. Things like infinite loops would leave sublime hanging.. Or if id wanna take input from keyoboard, it wouldn't work either. This bash script does exact same thing as far as compiling goes. But when it comes to running program it opens it in new terminal window.

@Ciaran0
Copy link

Ciaran0 commented Oct 23, 2012

I want to do what vgaldikas has done but for mac osx lion. Can anyone help?
Thanks

@mabelrxu
Copy link

If you look at the .sublime-buid for C++, for example, you can have multiple commands easily

{ "cmd": ["javac", "$file"], "file_regex": "^(..._?):([0-9]_):?([0-9]*)", "selector": "source.java",
"variants":
[
    {
        "name": "Run",
        "cmd": ["java", "${file_base_name}"]
    }
]

}

@keyeh
Copy link

keyeh commented Oct 3, 2013

use this bash script if you want it to automatically run in os x terminal

#!/bin/bash
# compiles all java files within directory and runs first in new terminal window.
for file in *.java
do
echo "Compiling $file"
javac $file
done
echo "Running $1.class"
echo -e "cd \`dirname \$0\`\nclear\njava $1\necho ________________________________________________________________________________\nread -p \042Program Terminated. [Press ENTER] to continue.\042\nrm -f sayhi.command" > sayhi.command; chmod +x sayhi.command; open sayhi.command;

@keyeh
Copy link

keyeh commented Oct 29, 2013

Updated, use this one:

#!/bin/bash
# compiles all java files within directory and runs the one being edited in new terminal window.



for file in *.java
do
echo "Compiling $file"
done
javac *.java
RETVAL=$?
[ $RETVAL -ne 0 ] && exit
[ $RETVAL -eq 0 ] && (
   echo "Compile Success! :)";
   echo "Running $1.class";

   #Create temporary file (in current directory) to execute compiled java class.
   #Deletes itself after execution.
   echo -e "cd \`dirname \$0\`\nclear\njava $1\necho ________________________________________________________________________________\nread -p \042Program Executed. [Press ENTER] to continue.\042\nrm -f sayhi.command" > sayhi.command;
   chmod +x sayhi.command; open sayhi.command;
   )

@ndamulelonemakh
Copy link

works well on my windows 8.1:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment