Sublime Text is a very popular editor for writing code. For Java, it supports compiling a Java source file with the default build named JavaC.Click Tools > Build System > JavaC to set the default build type for Java. Sublime text 2 is a great text editor. What makes it even better is the ability to compile and run Java code.Here is how to do:-1) Install sublime text 2 here. 2) Install the JDK (Java Developers Kit) from here. 3) After the JDK has been installed, locate the bin folder path in the JDK install directory.
Active2 months ago
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:
I have shell script(runJava.sh) as below, and I put it in the Java bin folder.
runJava.sh:
Java bin folder:
However, when I build a java file, I got an error message saying that..
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?
Chris Martin
24.6k44 gold badges5656 silver badges114114 bronze badges
stynerstyner
6 Answers
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:
Then use an editor to modify JavaC.sublime-build to add the following lines (don' forget the extra comma after the last line
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
AlexAlex
Easy step-by-step:
Build JSON definition:
steelsteel
6,58955 gold badges4848 silver badges7474 bronze badges
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:
After that, save, and while inside the java directory type:
How To Compile And Run Java In Sublime Text For Mac
$ 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.
BenSBenS
I modified mine to be as this
to run you do ctrl + shift + b
Chris Martin
24.6k44 gold badges5656 silver badges114114 bronze badges
Ynty WanadyYnty Wanady
Snippet software for windows. In Sublime Text 3, you just need simply click tools -> Build System -> New Build System and then paste the following from Sean Mullen:
Then Save it as java_run.sublime-build
How To Compile And Run Java In Sublime Text For Mac Download
That's it! When you click tools -> Build System again, you'll see java_run, choose this and then try to build, the java file will be compiled and run automatically.
Feng YiFeng Yi
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin is not in Sublime's path, as indicated by your error message. There are two ways to get around this. The first is to either move runJava.sh to /usr/bin , or create a symlink to it there. The second is to alter your JavaC.sublime-build file like so:
This adds your Java MattDMoMattDMo
bin folder to Sublime's PATH when the build system is executing, so that it can find runJava.sh . Adding 'shell': true runs the command through bash, which will allow it to pick up your other environment variables.
79.6k1717 gold badges180180 silver badges191191 bronze badges
Not the answer you're looking for? Browse other questions tagged javasublimetext3 or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |