1

I'm using Sublime Text 3 for writing and compiling my java programs but the output on Sublime console isn't helpful because we can't give inputs. I had found the sublime build code for C and C++ to show the output on cmd but haven't succeeded in Java. This is what I've written in my java.sublime-build file.

{
    "cmd": ["javac", "$file_name"],
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",

    "variants": [

        {
            "cmd": ["start", "cmd", "/k", "java", "$file_base_name"],
            "shell": true,
            "name": "Run"
        }
    ]
}

When I try to run the cmd opens but it isn't able to compile the java file and says, "Could not find or locate the main class". I want to run the program that I've just compiled in a cmd window (not in the Sublime console) What should I add to that command? Thank you.

G Ajeet
  • 27
  • 5

2 Answers2

0

I myself figured out my query and I am able to run my Java, C/C++, and Python programs and they are all showing the output in a pop-up command prompt and we can even run interactive programs in which programs take inputs.

I wanna share all those sublime-build codes with you all.

JAVA SUBLIME BUILD CODE

{
    "cmd": ["javac", "$file_name", "&&", "start","cmd", "/k", "java", "$file_base_name"],
    "shell": true,
    "quiet": true
}

C SUBLIME BUILD CODE

{
    "cmd": ["gcc.exe", "-o", "$file_base_name", "$file_name", "&&", "start", "cmd", "/k", "$file_base_name"],
    "shell": true,
    "quiet": true
}

C++ SUBLIME BUILD CODE

{
    "cmd": ["c++.exe", "-o", "$file_base_name", "$file_name", "&&", "start", "cmd", "/k", "$file_base_name"],
    "shell": true,
    "quiet": true
}

PYTHON SUBLIME BUILD CODE

{
    "cmd": ["start", "cmd", "/k", "python", "$file_name"],
    "shell": true,
    "quiet": true
}
G Ajeet
  • 27
  • 5
0

The answer above is perfect but if you just want to see the output in the sublime text console without any cmd pop-up, then you can use:

{
    "shell_cmd": "javac $file && java $file_base_name"
}

However, this is only for viewing output. It is not capable of accepting user inputs.

For those who don't know where to place this code, follow the steps:

Tools > Build System > New Build System ...

Then, paste whichever code snippet meets your requirements. Save the file as <filename>.sublime-build. Let's say runJava.sublime-build.

Then,

Tools > Build System > runJava.sublime-build

Then use Ctrl + B to compile and run the selected .java file.

You can also visit here, if you want the same process written in this answer along with screenshots.

risingStark
  • 1,153
  • 10
  • 17