Compiling and running Java code in Sublime Text 2

I am trying to compile and run Java code in Sublime Text 2. Do not just tell me to do this manually on the command line. Can anyone tell me how?

Btw, I'm on Windows 7 ...

+80
java compilation sublimetext2
May 12 '12 at 1:15
source share
19 answers

Perhaps your path does not work if you did not specify the correct folder - this is the step by step that I sent back, only for this version of the JDK.

If you don’t see any menu when you right-click on “Computer,” try this instead.

For the JDK version jdk1.7.0_07 Just update this with the location of your JDK when you release the new version.

  • Press "START"
  • Enter "Path" (without quotation marks) in the search field
  • You should see "Edit environment variables for your account" <--- click this
  • A window called "Environment Variables" should appear,
    • Press the TEMP button in the area above
    • Scroll a little in the lower area until you find the Path
    • Select Path and click Edit ...
  • Paste this at the very end of the bottom text area

;C:\Program Files\Java\jdk1.7.0_07\bin

  • Make sure OK from both windows
  • Reload sublime text if necessary

That is all that is needed.

So, to get the compilation and launch of your Java programs after completing the above, you will need to do the following. Just create a simple java class so that you are on the same page as me

Building a Java Class

  • Open a new SublimeText2 document and paste the following

     class hello { public static void main(String[] args) { System.out.println("Hello World!"); } } 
  • Then save this file on your desktop - name it

     hello.java 
  • you should have something like this Java and saved file

  • Now press Ctrl + b on the keyboard to create your own java class that should produce this! Build results

At last! - Launch your Java program!

  • You need to open a command prompt window, so go ahead and do it.
  • Then browse to the folder (in this case, our desktop) where your java class is located.
  • Command line navigation is simple - just use

     cd <-- this stands for change directory dir <-- this will list everything in the current directory if you get stuck! 
  • In my case, it looks like

dir and cd example

  • Cool, it looks like we're in the right place.
  • Finally enter

     java hello 

java hello result

I hope this helps anyone who stumbles about it!

+87
Oct 21 '12 at 16:52
source share

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" } ] } 

This means that it creates options for the regular build command ( ctrl + b ). With ctrl + b, you can still compile your code. If you do shift + ctrl + b , the first option will be executed, which in this case will be javac with the -Xlint option. The second and final option is the java command itself. you can place this as your first option, and shift + ctrl + b will actually execute the java code.

Also note that each option is a "name". This basically allows you to use this "build" option in the shift + ctrl + p option. Therefore, using this configuration, you can simply do shift + ctrl + p and type “Run Java” and press enter , and your code will be executed.

Hope this helped.

+77
Nov 21 '12 at 7:40
source share

I find a method in post Compiling and running Java programs with Sublime Text 2 works well and is a bit more convenient than other methods. Here is a link to an 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 the jdk bin directory.

Step 2:

  • Open the Sublime package directory using Settings> Package Overview.
  • Go to Java folder
  • Open JavaC.sublime-build and replace the line
    "cmd": ["javac", "$file"],
    from
    "cmd": ["runJava.bat", "$file"],

Done!

Write programs and execute with CTRL + B

Note. The instructions for Sublime 3 are different.

+19
Feb 11 '13 at 10:24
source share

Sublime Text 3 has a slightly different solution. This is a modification of the vijay answer I used before.

  { "shell_cmd": "javac -Xlint \"${file}\"", "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "working_dir": "${file_path}", "selector": "source.java", "variants": [ { "name": "Run", "shell_cmd": "java \"${file_base_name}\"" } ] } 

Paste the above into a new file called JavaC.sublime-build and put it in your custom packages. This can be found in C:\Users\You\AppData\Roaming\Sublime Text 3\Packages\User .

Ctrl-B will compile. This will launch Ctrl-Shift-B.

+15
Oct 10 '14 at 16:57
source share

This version of JavaC.sublime-build, which is edited from a vijay answer, works for me on both Windows 7 and Mac for Sublime Text 3.

I edited it so that Ctrl + b or the + b command were enough to build + run.

 { "shell_cmd": "javac -Xlint $file && java $file_base_name", "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "selector": "source.java", "shell": true } 

'& &' guarantees that the second part will work only when the first part is successful, i.e. only when creating a class file. You can find more information here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true

+8
Aug 26 '15 at 15:05
source share

I am using Windows 7. The solution below works for me.

 **Open** the file JavaC.sublime-build and replace all the code in the file with the code below: { "cmd": ["javac", "$file_name","&&","java", "$file_base_name"], "file_regex": "^(...*?):([0-9]*):?([0-9]*)", **"path": "C:\\Program Files\\Java\\jdk1.6.0\\bin\\",** "selector": "source.java", "shell": true } 

Remember to replace "C: \ Program Files \ Java \ jdk1.6.0 \ bin \" with the path where you put your jdk. And don't forget to add the java JDK path to your PATH environment variable. Refer to the bunnyDrug post to set up the environment variable. Best!!

+6
Sep 28 '13 at 19:19
source share

compile and run in accordance with the sublime text documentation 2 3rd

step-1: set environment variables for java since you already know or reference somewhere

STRP-2: open a new document and copy the paste code below

 { "cmd": ["javac", "$file_name", "&&", "java", "$file_base_name"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.java", "shell":true } 

step 3: save the document as userjavaC.sublime-build in the directory C: \ Users \ myLapi \ AppData \ Roaming \ Sublime Text 3 \ Packages \ User

step 4:

after making a selection as tools-> build systems-> userjavaC

to compile and run, press ctrl + b

+6
Jan 14 '16 at 11:59
source share

Refer to the solution at: http://www.compilr.org/compile-and-run-java-programs/

Hope that solves both compiling and running classes inside sublime ..... You can see my script in the comments section to try it in case of mac ...

EDIT: Sorry, the above link is now broken. It details all the steps required to compile and run java in exalted text. In any case, for Mac or Linux systems, the following should work:

edit javac.sublime-build file for:




 #!/bin/sh classesDir="/Users/$USER/Desktop/java/classes/" codeDir="/Users/$USER/Desktop/java/code/" [ -f "$classesDir/$1.class" ] && rm $classesDir/$1.class for file in $1.java do echo "Compiling $file........" javac -d $classesDir $codeDir/$file done if [ -f "$classesDir/$1.class" ] then echo "-----------OUTPUT-----------" java -cp $classesDir $1 else echo " " fi 



Here I made a folder with the name "java" on the desktop and the subfolders "classes" and "code" to support .class and .java files, respectively, you can change it in your own way.

+3
Feb 25 '13 at 14:08
source share

As described here:

http://sublimetext.userecho.com/topic/90531-default-java-build-system-update/

The steps I took to fix this

  • Click Start

  • Right click on "Computer"

2.5 Click Properties.

  • On the left side, select "Advanced system settings"

  • Next to the bottom click on "Environment Variables"

  • Scroll down to "System Variables" until you find "PATH" - click "Edit" with the selected.

  • Add the path to the bin bin folder. Mine looks like this:

    CODE: SELECT ALL

    ; C: \ Program Files \ Java \ jdk1.7.0_03 \ bin \

+2
May 12 '12 at 1:27
source share

For sublime text 3
in "C: \ Program Files \ Sublime Text 3 \ Packages" you get java.sublime-package, copy it to another folder, change its extension from .sublime-package to zip and extract it, you will get the JavaC.sublime-build file for your modifications as above. after all the changes extracted from the java folder, convert it to .zip again and change its .zip extension to .sublime-package. after that, copy and paste this file into C: \ Program Files \ Sublime Text 3 \ Packages.
it will help you!

(even you get my file from http://upfile.mobi/363820 or http://www.4shared.com/file/73SkzY-Kba/Java.html link I use java code to run, I use the Wesley trick Baugh ", so you need to copy the runJava.bat file to the C: \ Program Files (x86) \ Java \ jdk1.8.0 \ bin directory as it says. And copy the above linked file to C: \ Program Files \ Sublime Text 3 \ Packages)

+2
Jan 30 '14 at 13:01
source share

You can completely compile and run your code in ST, and it is very fast / simple. There's a recent ST package called Javatar that can do this. https://javatar.readthedocs.org

+2
Jul 22 '14 at 15:24
source share

This is the code to compile and run java in exalted text 3

"shell_cmd": "javac -d. $ file & java $ {file_base_name}. $ {file_base_name}", "shell": true

+2
May 24 '17 at 18:26
source share

I followed the post in this thread and worked perfectly:

Make the bat file with the following and save it anywhere on your PATH. I suggest C: \ Program Files \ Java \ jdk * \ bin \ save everything together.

 @ECHO OFF cd %~dp1 javac %~nx1 java %~n1 

then edit C: \ Users \ your_user_name \ AppData \ Roaming \ Sublime Text 2 \ Packages \ Java \ JavaC.sublime-build, the content will be

 { "cmd": ["javac", "$file"], "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "selector": "source.java" } 

replace "javac" with the name of your bat file (for example, javacexec.bat) and save it.

+1
Feb 07 '14 at 23:17
source share

Following the instructions below, you will receive 2 Build Systems in an elevated form - "JavaC" and "JavaC_Input".

"JavaC" will allow you to run code that does not require user input and display the results in a great terminal simulator that is convenient and beautiful. "JavaC_Input" allows you to run code that requires user input in a separate terminal window, it can accept user input. You can also run code that does not require input in this assembly, so if you are not against a pop-up window, you can just stick to this assembly system and not switch. You switch between build systems from Tools → Build System. And you compile and run the code with ctrl + b.

Here are the steps for doing this:

(note: make sure you already have a basic java system setup: install the JDK and configure the correct CLASSPATH and PATH, I will not dwell on this in detail)

Setting up the JavaC build system

1. Make a bat file with the following code and save it in the C: \ Program Files \ Java \ jdk * \ bin \ folder to save everything together. Name the file "javacexec.bat".

 @ECHO OFF cd %~dp1 javac %~nx1 java %~n1 

2, then edit C: \ Users \ your_user_name \ AppData \ Roaming \ Sublime Text 2 \ Packages \ Java \ JavaC.sublime-build (if not, create one), the content will be

 { "cmd": ["javacexec.bat", "$file"], "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "selector": "source.java" } 

"Configuring the JavaC_Input Build System"

1, install Cygwin [ http://www.cygwin.com/]

2, go to C: \ Users \ your_user_name \ AppData \ Roaming \ Sublime Text 2 \ Packages \ Java \, then create a file called JavaC_Input.sublime-build with the following contents

 { "cmd": ["javacexec_input.bat", "$file"], "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "selector": "source.java" } 

3. Make a bat file with the following code and save it in the C: \ Program Files \ Java \ jdk * \ bin \ folder to save everything together. Name the file "javacexec_input.bat".

 @echo off javac -Xlint:unchecked %~n1.java start cmd /k java -ea %~n1 
+1
Jan 24 '17 at 5:47
source share

Make sure you install JDK / JRE first.

If you are a Mac user, follow these steps:

Open a terminal, go to your root dictionary by typing

cd ..

repeatedly. use

ls

to see if u has reached the root

you will see the library folder. Now follow this path. Library / Java / JVM / bin Once you get into the trash, you will see the JavaC file

Now you need to get the path to this folder to just write this command

Pwd

Copy it into your exalted JavaC file and create java code in sublime by cmd + b.

0
Apr 14 '15 at 2:15
source share

Alex Yao's answer is the simplest solution, if you just want to build and run a Java program without entering data from the console, use the solution provided by Alex Yao . However, if you want to get input from the console, go to the following link

Java console input in Sublime Text 2?

0
Aug 12 '18 at 13:28
source share

This is mine using sublime text 3. I needed the ability to open the command line in a new window. -Xlint Java is used with -Xlint to enable full warning messages in Java.

I saved the file in a custom package folder as Java (Build) .sublime-build

 { "shell_cmd": "javac -Xlint \"${file}\"", "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "working_dir": "${file_path}", "selector": "source.java", "variants": [ { "name": "Run", "shell_cmd": "java \"${file_base_name}\"", }, { "name": "Run (External CMD Window)", "shell_cmd": "start cmd /k java \"${file_base_name}\"" } ] } 
0
Sep 12 '18 at 2:07
source share

The JavaC build system works like a charm, but fails if you want to pass input from stdin to Sublime-text. But you can edit the build system so that it receives data from the user. This is a modified JavaC build that I use in Ubuntu 18.04 LTS. You can edit the build system or create a new build system.

Create a new build system.

  • Go to Tools >> Build System >> New Build System .
  • Copy Paste the code below and File >> Save .

    {

     "shell_cmd": "javac \"$file\"", "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "selector": "source.java", "variants": [ { "shell_cmd":"bash -c \"javac $file\" && gnome-terminal -- bash -c \"java $file_base_name ;read\"", "name": "Run" } ] 

    }

Edit Existing Java C Build File

  • Follow the instructions for https://stackoverflow.com/a/167708/
  • Copy and paste the "options" from this build system.
0
Dec 29 '18 at 19:12
source share
 { "shell_cmd": "javac -Xlint \"${file}\"", "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "working_dir": "${file_path}", "selector": "source.java", "variants": [ { "shell_cmd":"javac -Xlint \"${file}\" && java $file_base_name < input.txt > output.txt", "name": "Run" } ] } 

save this sublime assembly and run the program using ctrl + shift + B with a run option. Without the launch option, it will simply create a .class file, but will not run it.

This assembly will read the input from input.txt and print the output to output.txt.

Note : both input.txt and output.txt must be present in the same working directory as your .java file.

0
Jun 13 '19 at 17:52
source share



All Articles