How to run Java using Sublime Text 3 on Mac OS

I recently met Sublime Text 3 and tried to run Java in it.

I changed JavaC.sublime-build in the package, Java.sublime-package.

JavaC.sublime builds:

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

I have a shell script (runJava.sh) as shown below and I put it in the bin bin folder.

runJava.sh:

 [ -f "$1.class" ] && rm $1.class for file in $1.java do echo "Compiling $file........" javac $file done if [ -f "$1.class" ] then echo "-----------OUTPUT-----------" java $1 else echo " " fi 

Bin bin folder:

 /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin 

However, when I create the java file, I got an error message saying that ....

 [Errno 2] No such file or directory: 'runJava.sh' [cmd: ['runJava.sh', 'Test']] [dir: /Users/xxxxxxx/Desktop] [path: /usr/bin:/bin:/usr/sbin:/sbin] [Finished] 

I have no idea why this message appeared, because runJava.sh is in the right place, this is the bin bin folder.

What is the cause of this problem?

+7
java sublimetext3
source share
7 answers

This is because you do not have to modify the package files directly with the editor. These are zip files, although they do not have an extension to alert the user.

To change the plugin, you need to go to / Packages and unzip the Java.sublime-package file. I will use Linux syntax, but the procedure will remain the same:

 cd <sublime-text3-folder>/Packages mkdir java cp Java.sublime-packages java cd java unzip Java.sublime-packages 

Then use the editor to modify JavaC.sublime-build to add the following lines (don't forget the extra comma after the last line

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

Update the content in the Java.sublime package and return it to the Package folder via:

zip Java.sublime-package * cp Java.sublime-package ..// packages

Restart sublime and now, together with Ctrl + B, to build the project, you can start it with Ctrl + Mayus + B

+12
source share

Easy step by step:

  1. Install the management pack if you haven’t already

  2. Using Package Control, install Package Resource Viewer

  3. Open Package Control, enter prv and run PackageResourceViewer: Open Resource

  4. Choose Java

  5. Choose JavaC.sublime-build

  6. Exactly replace the assembly JSON definition with the code block below

  7. You did it! ctrl+b should now compile and run your Java file.

Build a JSON definition:

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

There are many solutions that say the same thing without providing step-by-step instructions. So here is my experience on Mac'16 with Sublime Text 3 :

Open a terminal window and do the following:

$ cd / Applications / Sublime \ Text.app/Contents/MacOS/Packages/

Made a temporary directory to link.

$ mkdir java

Copy the current Java.sublime package to the new java directory and go to java.

$ cp Java.sublime-package java /

$ cd java

Then unzip the package to see the contents:

$ unzip Java.sublime-package

Now edit the JavaC.sublime-build assembly file. If you have an elevated text command line script:

$ subl JavaC.sublime-build

Otherwise

$ vi JavaC.sublime-build

Then I copied the following into my JavaC.sublime-build, which I found here, published by Sean Mullen :

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

After that, save and, being inside the java directory, type:

$ zip Java.sublime-package *

Move the new assembly package to the desired folder (parent directory):

$ mv Java.sublime-package ../

UPDATE: 04/18/18: Sorry for taking so long to correct this answer. Based on @bumbu's comments, we need to run the following command before deleting the temporary directory:

$ cd ..

Delete Java temporary directory:

$ rm -fr java /

AND DONE. Now building the Java file will also try to run it. I prefer this behavior instead of making another "option" to run the program.

+7
source share

I modified mine to be like that

to execute ctrl + shift + b

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

In Sublime Text 3, you just need to click “ Tools” → “Build System” → “New Build System”, and then paste the following from Sean Mullen :

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

Then save it as java_run.sublime-build

It! When you click Tools → Build system again, you will see java_run, select this and then try to build, the java file will be compiled and launched automatically.

enter image description here

+1
source share

/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin not in the Sublime path, as indicated by your error message. There are two ways around this. The first is to either move runJava.sh to /usr/bin , or create a symbolic link there. The second way is to modify your JavaC.sublime-build file as follows:

 { "cmd": ["runJava.sh", "$file_base_name"], "path": "/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin:$PATH", "shell": true, "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "selector": "source.java" } 

This adds your Java bin to the Sublime PATH when the build system runs so that it can find runJava.sh . Adding "shell": true runs the command through bash, which allows it to display other environment variables.

0
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
source share

All Articles