Compiling and Running Java Code in Sublime Text 2

前端 未结 19 2488
执念已碎
执念已碎 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 02:09

    This is code to compile and run java in sublime text 3

    "shell_cmd": "javac -d . $file && java ${file_base_name}.${file_base_name}", "shell": true

    0 讨论(0)
  • 2020-11-28 02:10

    Refer the solution at: http://www.compilr.org/compile-and-run-java-programs/

    Hope that solves, for both compiling and running the classes within sublime..... You can see my script in the comments section to try it out in case of mac...

    EDIT: Unfortunately, the above link is broken now. It detailed all the steps required for comiling and running java within sublime text. Anyways, for mac or linux systems, the below should work:

    modify javac.sublime-build file to:


    #!/bin/sh
    
    classesDir="/Users/$USER/Desktop/java/classes/"
    codeDir="/Users/$USER/Desktop/java/code/"
    [ -f "$classesDir/$1.class" ] && rm $classesDir/$1.class
    for file in $1.java
    do
    echo "Compiling $file........"
    javac -d $classesDir $codeDir/$file
    done
    if [ -f "$classesDir/$1.class" ]
    then
    echo "-----------OUTPUT-----------"
    java -cp $classesDir $1
    else
    echo " "
    fi
    

    Here, I have made a folder named "java" on the Desktop and subfolders "classes" and "code" for maintaining the .class and .java files respectively, you can modify in your own way.

    0 讨论(0)
  • 2020-11-28 02:12

    This is mine using sublime text 3. I needed the option to open the command prompt in a new window. Java compile is used with the -Xlint option to turn on full messages for warnings in Java.

    I have saved the file in my user package folder as Java(Build).sublime-build

    {
         "shell_cmd": "javac -Xlint \"${file}\"",
         "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
         "working_dir": "${file_path}",
         "selector": "source.java",
         "variants":
         [
              {
                   "name": "Run",
                    "shell_cmd": "java \"${file_base_name}\"",
              },
              {
                   "name": "Run (External CMD Window)",
                   "shell_cmd": "start cmd /k java \"${file_base_name}\""
              }
         ]
    }
    
    0 讨论(0)
  • 2020-11-28 02:18

    As detailed here:

    http://sublimetext.userecho.com/topic/90531-default-java-build-system-update/

    Steps I took to remedy this

    1. Click Start

    2. Right click on 'Computer'

    2.5 Click Properties.

    1. On the left hand side select 'Advanced System Settings'

    2. Near the bottom click on 'Environment Variables'

    3. Scroll down on 'System Variables' until you find 'PATH' - click edit with this selected.

    4. Add the path to your Java bin folder. Mine ends up looking like this:

      CODE: SELECT ALL

      ;C:\Program Files\Java\jdk1.7.0_03\bin\

    0 讨论(0)
  • 2020-11-28 02:18
    {
    "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.

    0 讨论(0)
  • 2020-11-28 02:19

    So this is what i added to the JavaC.sublime-build file

    {
        "cmd": ["javac", "-Xlint", "$file"],
        "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
        "selector": "source.java",
    
        "variants": [
    
            { "cmd": ["javac", "-Xlint", "$file"],
              "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
              "selector": "source.java",
              "name": "Java Lintter"
            },  
    
            { "cmd": ["java", "$file_base_name"],
              "name": "Run Java"
            }
        ]
    }
    

    What this does is that it creates variants to the regular build command (ctrl+b). With ctrl+b you will still be able to compile your code. If you do shift+ctrl+b the first variant will be executed, which in this case is javac with the -Xlint option. The second and final variant is the java command itself. you can place this as your first variant and shift+ctrl+b will actually execute the java code.

    Also, notice that each variant as a "name". This basically allows this specific "build" option to show up in the shift+ctrl+p option. So using this configuration, you can simply do shift+ctrl+p and type "Run Java" and hit enter, and your code will execute.

    Hope this helped.

    0 讨论(0)
提交回复
热议问题