Compiling and Running Java Code in Sublime Text 2

前端 未结 19 2821
执念已碎
执念已碎 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条回答
  •  旧时难觅i
    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.

提交回复
热议问题