Recently I've decided to learn Java and give it a try. I have a short and amateur experience with python, therefore I'm not a kind of programming expert. After many days trying to figure out how to set up Sublime Text to run and compile Java, I've decided to come for any help.
I've already installed JDK and I was using Netbeans, but I prefer a minimalistic IDE.
I created a .sublime-build file like this below..
However when I try to build the code, it returns me that error below..
So, probably I've done something wrong, but I can't really figure out what it is. I'm using Ubuntu 14.10 and that's the reason I cannot find many answers out there, so I'd really appreciate if anyone could help me out!
Tested on Ubuntu 16.04 LTS with Sublime Text3. Make your own sublime-build system, and enter this code:
{
"shell_cmd": "javac \"$file\" && java \"$file_base_name\"",
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java",
}
This will let you first compile the file AND run the class based on the file name together. After this, you will see the result of string "Hello World"
Look at https://gist.github.com/jfcalvo/3789664 their solution is
JavaC.sublime-build
{
"cmd": ["javac \"$file_name\" && java \"$file_base_name\""],
"shell": true,
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
much like J. Seo, and Mad Physicist above, only with "shell":true added.
Create two files in the directory of your source file:
input.txt
- It is for giving user inputs. output.txt
- It is for storing the outputs(you can use the built-in terminal as well).Go to Tools > Build System > New Build System.
Now, write the following code and save it as a new .sublime-build
file.
customJava.sublime-build
Now, select the new build system, provide inputs(if required) and run the code.(Ctr+B)
Use this build.
{
"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 build and run the program with ctrl + shift + B
with run variant. Without run variant it will just create .class
file but wont run it.
This build will read the input from input.txt
and print the output in output.txt
.
Note: Both input.txt
and output.txt
must be present in the same working directory as your .java
file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With