I am trying to compile and run Java code in Sublime Text 2. Don't just tell me to do it manually in the Command Prompt. Can anyone tell me how?
Btw, I am on Windows 7...
java file click on Tools header. Click Build System and select javaC. Now you are ready you can build program with Ctrl+B shortcut or go to Tools and press Build. Once compilation has succeed you can run program by pressing java under Build System of Tools.
Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.
So this is what i added to the JavaC.sublime-build file
{ "cmd": ["javac", "-Xlint", "$file"], "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "selector": "source.java", "variants": [ { "cmd": ["javac", "-Xlint", "$file"], "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "selector": "source.java", "name": "Java Lintter" }, { "cmd": ["java", "$file_base_name"], "name": "Run Java" } ] }
What this does is that it creates variants to the regular build command (ctrl+b). With ctrl+b you will still be able to compile your code. If you do shift+ctrl+b the first variant will be executed, which in this case is javac with the -Xlint option. The second and final variant is the java command itself. you can place this as your first variant and shift+ctrl+b will actually execute the java code.
Also, notice that each variant as a "name". This basically allows this specific "build" option to show up in the shift+ctrl+p option. So using this configuration, you can simply do shift+ctrl+p and type "Run Java" and hit enter, and your code will execute.
Hope this helped.
I find the method in the post Compile and Run Java programs with Sublime Text 2 works well and is a little more convenient than the other methods. Here is a link to the archived page.
Create runJava.bat
with the following code.
@ECHO OFF cd %~dp1 ECHO Compiling %~nx1....... IF EXIST %~n1.class ( DEL %~n1.class ) javac %~nx1 IF EXIST %~n1.class ( ECHO -----------OUTPUT----------- java %~n1 )
Copy this file to jdk bin directory.
"cmd": ["javac", "$file"],
"cmd": ["runJava.bat", "$file"],
Done!
Write programs and Run using CTRL + B
Note: Instructions are different for Sublime 3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With