Compiling and Running Java Code in Sublime Text 2

前端 未结 19 2487
执念已碎
执念已碎 2020-11-28 01:20

I am trying to compile and run Java code in Sublime Text 2. Don\'t just tell me to do it manually in the Command Prompt. Can anyone tell me how?

Btw, I am on Windows

相关标签:
19条回答
  • 2020-11-28 01:54

    I find the method in the post Compile and Run Java programs with Sublime Text 2 works well and is a little more convenient than the other methods. Here is a link to the archived page.

    For Windows:

    Step 1:

    Create runJava.bat with the following code.

    @ECHO OFF
    cd %~dp1
    ECHO Compiling %~nx1.......
    IF EXIST %~n1.class (
    DEL %~n1.class
    )
    javac %~nx1
    IF EXIST %~n1.class (
    ECHO -----------OUTPUT-----------
    java %~n1
    )
    

    Copy this file to jdk bin directory.

    Step 2:

    1. Open Sublime package directory using Preferences > Browse Packages..
    2. Go to Java Folder
    3. Open JavaC.sublime-build and replace line
      "cmd": ["javac", "$file"],
      with
      "cmd": ["runJava.bat", "$file"],

    Done!

    Write programs and Run using CTRL + B

    Note: Instructions are different for Sublime 3.

    0 讨论(0)
  • 2020-11-28 01:55

    This is how I did it with these easy steps:

    Setup a new build system:

    1. Tools > Build System > New Build System

    2. Replace the default code with the following:

      {
         "cmd": ["javac","$file_name","&&","java","$file_base_name"], 
         "path": "C:\\Program Files\\Java\\jdk1.7.0_25\\bin\\", 
         "shell": true
      }
      // locate the path of your jdk installation and replace it with 'path' 
      
    3. Save the file by giving it a name (I named mine "Java")

    Activate the build system:

    1. Tools > Build System > Java (name of the file you saved it with)
    2. Now run your program with Ctrl + B
    0 讨论(0)
  • 2020-11-28 01:56

    By following the steps below, you will have 2 Build Systems in sublime - "JavaC" and "JavaC_Input".

    "JavaC" would let you run code that doesn't require user input and display the results in sublime's terminal simulator, which is convenient and nice-looking. "JavaC_Input" lets you run code that requires user input in a separate terminal window, it's able to accept user input. You can also run non-input-requiring code in this build system, so if you don't mind the pop-up, you can just stick with this build system and don't switch. You switch between build systems from Tools -> Build System. And you compile&run code using ctrl+b.

    Here are the steps to achieve this:

    (note: Make sure you already have the basic setup of the java system: install JDK and set up correct CLASSPATH and PATH, I won't elaborate on this)

    "JavaC" build system setup

    1, Make a bat file with the following code, and save it under C:\Program Files\Java\jdk*\bin\ to keep everything together. Name the file "javacexec.bat".

    @ECHO OFF
    cd %~dp1
    javac %~nx1
    java %~n1
    

    2, Then edit C:\Users\your_user_name\AppData\Roaming\Sublime Text 2\Packages\Java\JavaC.sublime-build (if there isn't any, create one), the contents will be

    {
       "cmd": ["javacexec.bat", "$file"],
       "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
       "selector": "source.java"
    }
    

    "JavaC_Input" build system setup

    1, Install Cygwin [http://www.cygwin.com/]

    2, Go to C:\Users\your_user_name\AppData\Roaming\Sublime Text 2\Packages\Java\, then create a file called "JavaC_Input.sublime-build" with the following content

    {
    "cmd": ["javacexec_input.bat", "$file"],
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.java"
    }
    

    3, Make a bat file with the following code, and save it under C:\Program Files\Java\jdk*\bin\ to keep everything together. Name the file "javacexec_input.bat".

    @echo off
    javac  -Xlint:unchecked %~n1.java 
    start cmd /k java -ea %~n1
    
    0 讨论(0)
  • 2020-11-28 01:57

    This version of JavaC.sublime-build which is edited from vijay's answer works for me on both Windows 7 and Mac for Sublime Text 3.

    I edited it so Ctrl+b or command+b is sufficient for both build + run.

    {
    "shell_cmd": "javac -Xlint $file && java $file_base_name",
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.java",
    "shell": true
    }
    

    '&&' ensures that the second part runs only when first part succeeds ie only when the class file is generated. You can find more related info here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true

    0 讨论(0)
  • 2020-11-28 01:58

    Make sure u install JDK/JRE first.

    If you are mac user than follow this steps:

    open terminal go to your root dictionary by typing

    cd ..

    repeatedly. use

    ls

    to see if u have reach the root

    you will see Library folder Now follow this path Library/Java/JVM/bin Once you get into bin you can see JavaC file

    Now u need to get the path of this folder for that just write this command

    pwd

    Copy paste it to your sublime JavaC file and build java code in sublime by cmd+b.

    0 讨论(0)
  • 2020-11-28 01:58

    The Build System JavaC works like a charm but fails when you want to give input from stdin in Sublime-text. But you can edit the build system to make it receive input from user. This is the modified JavaC build I'm using on Ubuntu 18.04 LTS. You can edit the build System or create a new build system.

    To Create a new build system.

    • Go to Tools>>Build System>>New Build System.
    • Copy Paste the Below code and File>>Save.

      {

      "shell_cmd": "javac \"$file\"",
      "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
      "selector": "source.java",
      "variants": 
      [
          {
              "shell_cmd":"bash -c \"javac $file\" && gnome-terminal -- bash -c \"java $file_base_name ;read\"", 
              "name": "Run"
          }
      
      ]    
      

      }

    To Edit the existing Java C build file

    • Follow the steps https://stackoverflow.com/a/23790095/10847305
    • Copy-Paste "variants" from this build system.
    0 讨论(0)
提交回复
热议问题