Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling and Running Java Code in Sublime Text 2

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

like image 729
tolluy Avatar asked May 12 '12 01:05

tolluy


People also ask

How do I compile and run Java program in sublime text?

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.

How do I compile and run Java code?

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.


2 Answers

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.

like image 94
vijay Avatar answered Sep 20 '22 13:09

vijay


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.

For Windows:

Step 1:

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.

Step 2:

  1. Open Sublime package directory using Preferences > Browse Packages..
  2. Go to Java Folder
  3. Open JavaC.sublime-build and replace line
    "cmd": ["javac", "$file"],
    with
    "cmd": ["runJava.bat", "$file"],

Done!

Write programs and Run using CTRL + B

Note: Instructions are different for Sublime 3.

like image 21
vancexu Avatar answered Sep 18 '22 13:09

vancexu