Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up Sublime text 3 to run and compile java on linux?

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.. Java sublime.build

However when I try to build the code, it returns me that error below.. Java Error

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!

like image 835
wombatp Avatar asked Feb 09 '15 18:02

wombatp


4 Answers

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"

Result

like image 194
J.Seo Avatar answered Oct 21 '22 19:10

J.Seo


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.

like image 37
user1420482 Avatar answered Oct 21 '22 19:10

user1420482


  1. 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).
  2. 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

  3. Now, select the new build system, provide inputs(if required) and run the code.(Ctr+B)

like image 1
gaurabdg Avatar answered Oct 21 '22 20:10

gaurabdg


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.

like image 1
akhil Avatar answered Oct 21 '22 21:10

akhil