'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.
Solution 1:[1]
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
}
Solution 2:[2]
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.
Solution 3:[3]
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\"",
}
]}
Solution 4:[4]
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.
Solution 5:[5]
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"
}
Solution 6:[6]
For Windows:
{
"cmd": ["java", "$file"]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MattDMo |
| Solution 2 | Pravin R |
| Solution 3 | |
| Solution 4 | tushortz |
| Solution 5 | wellsemon |
| Solution 6 | Lakshya Sharma |
