Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Java using Sublime Text 3 on Mac OS

Recently, I have got to know Sublime Text 3, and tried to run Java in it.

I have modified JavaC.sublime-build in the package, Java.sublime-package.

JavaC.sublime-build:

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

I have shell script(runJava.sh) as below, and I put it in the Java 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

Java bin folder:

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

However, when I build a 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 showed up, because runJava.sh is located in the right place, which is the Java bin folder.

What is the reason of this problem?

like image 472
styner Avatar asked Jun 20 '14 02:06

styner


3 Answers

Easy step-by-step:

  1. Install Package Control if you have't already

  2. Using Package Control, install Package Resource Viewer

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

  4. Select Java

  5. Select JavaC.sublime-build

  6. Replace the build JSON definition with the below code block exactly

  7. You're done! ctrl+b should now compile and run your Java file.

Build JSON definition:

{
  "cmd": ["javac \"$file_name\" && java \"$file_base_name\""],
  "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
  "selector": "source.java",
  "shell": true
}
like image 72
steel Avatar answered Nov 17 '22 03:11

steel


That's because you are not supposed to modify package files directly with an editor. They are zip files, despite they don't have an extension to warn the user.

To modify the plug-in you have to go to /Packages and unzip the Java.sublime-package file. I'll use linux syntax but the procedure remains the same:

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

Then use an editor to modify JavaC.sublime-build to add the following lines (don' 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"
       }
   ]
}

Zip again the contents in Java.sublime-package and put it back in Package folder via:

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

Restart sublime and now along with Ctrl+B to build your project you will be able to run it with Ctrl + Mayus + B

like image 12
Alex Avatar answered Nov 17 '22 03:11

Alex


There are many solutions out there that kind of say the same thing without providing a step-by-step. So here was my experience on a Mac'16 with Sublime Text 3:

Open a terminal window and do the following:

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

Made a temp directory to mess with.

$ mkdir java

Copy current Java.sublime-package into new java directory and cd into java.

$ cp Java.sublime-package java/

$ cd java

Then, unzip the package to see the contents:

$ unzip Java.sublime-package

Now, edit the build file, JavaC.sublime-build. If you have sublime text's 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 posted 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 while inside the java directory type:

$ zip Java.sublime-package *

Move new build package to necessary folder (parent directory):

$ mv Java.sublime-package ../

UPDATE: 4/18/18: Sorry it took me so long to adjust this answer. Based on comments from @bumbu, we do need to execute the following command before removing the temp directory:

$ cd ..

Remove temp java directory:

$ rm -fr java/

And DONE. Now building the Java file will now attempt to run it as well. I prefer this behavior instead of having to do another "variant" to run the program.

like image 9
BenS Avatar answered Nov 17 '22 02:11

BenS