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

∥☆過路亽.° 提交于 2019-12-20 16:20:39

问题


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!


回答1:


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"




回答2:


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.




回答3:


  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)




回答4:


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.



来源:https://stackoverflow.com/questions/28416885/how-to-set-up-sublime-text-3-to-run-and-compile-java-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!