问题
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.
回答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
}
回答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.
回答3:
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.
回答4:
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"
}
来源:https://stackoverflow.com/questions/32960540/java-build-in-sublime-3