Ethan Hamilton
Ethan Hamilton

Reputation: 31

Java Build in Sublime 3

I'm trying to set up a build so that I can compile and run Java in sublime 3. My build system isn't working.

"cmd": "java ${file_name} && java ${file_base_name}",
"file_regex": "^[ ]file \"(...?)\", line ([0-9]*)",
"path":"C:\\ProgramData\\Oracle\\Java\\javapath\\java.exe",
"selector": "source.java",
"shell":true

I have this build saved to my packages and it appears in the build menu, but when I try and run it, it says that there is no build working.

Upvotes: 2

Views: 10092

Answers (6)

Aditya Dolas
Aditya Dolas

Reputation: 31

Try this. It worked for me on Mac.

Step 1: Go to Sublime Text Preferences and click on Browse Packages.

Step 2: Create Java folder if there is no Java folder.

Step 3: Create JavaC.sublime-build and add following code and save it.

{
  "shell_cmd": "javac \"$file_name\" && java \"$file_base_name\"",
  "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
  "selector": "source.java",

  "variants":
   [
     {
       "name": "Syntax Check",
       "shell_cmd": "javac \"$file_name\" && java \"$file_base_name\"",
     }
   ]
}

Upvotes: 3

Lakshya Sharma
Lakshya Sharma

Reputation: 11

For Windows:

{
"cmd": ["java", "$file"]
}

Upvotes: 0

wellsemon
wellsemon

Reputation: 1

JavaC.sublime-build sample to set java classpath path when running java code.

{
  "shell_cmd": "javac -encoding utf-8 $file_name && java -cp .:/path/* $file_base_name && rm -rf $file_base_name.class",
  "file_regex": "^ *\\[javac\\] (.+):([0-9]+):() (.*)$",
  "selector": "source.java",
  "encoding": "utf-8"
}

Upvotes: 0

Pravin R
Pravin R

Reputation: 21

Try this:

{
    "file_regex": "(.+):(\\d+): error: ",
    "shell_cmd": "javac $file && java $file_base_name",
    "shell": true,
    "selector": "source.java",
}

its working on windows sublime text 3.

Upvotes: 2

tushortz
tushortz

Reputation: 5025

Try this. It works on linux. Not sure about Windows.

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

Upvotes: 0

MattDMo
MattDMo

Reputation: 102852

Please read the build system documentation for information on how to structure .sublime-build files. "cmd" and "path" are lists, not strings, so that's one reason you may be getting an error. Another reason, as I mentioned in the comments, is that you are trying to compile your .java file with java instead of javac. Finally, your "path" is incorrect — it should point to a list of directories, not files. This may work better for you:

{
    "cmd": ["javac", "${file_name}", "&&", "java", "${file_base_name}"],
    "file_regex": "^[ ]file \"(...?)\", line ([0-9]*)",
    "path": ["C:\\ProgramData\\Oracle\\Java\\javapath"],
    "selector": "source.java",
    "shell": true
}

Upvotes: 3

Related Questions