Sublime Forum

How to have input Java?

#1

Hi,
I am having a big trouble getting input in my java programs. I know that natively Sublime Text 3 doesn’t take in input.

But is there a good way to run my java programs so that I can have input ?

For the info I am on Mac, and have gone trough a dozen of build systems found on the internet and couples hour of research, with no success.

0 Likes

Sublime text built-in console
#2

I’d just use a terminal. Is that not sufficient or are you looking for a way to do so specifically in Sublime Text?

2 Likes

#3

You can try the Terminus package. It brings console like features to ST3 and can be configured as build system as well. The README comes along with some good examples how to do so.

0 Likes

#4

Thanks, I will try it out.

0 Likes

#5

I have tried using the terminal, but it was kind of clunky having to type in the folder address and the class name, even tough you can bring it back after you have typed it once it is kinda of pain.

The biggest problem for me with the terminal is that I couldn’t run a class that depended on an other class, but maybe that’s just me… our teacher had made a class that made it more simple do draw figures, in the terminal I could’t run a class that used what he had prepared… So I used something else.

The best would be some kind of way to just push the build shortcut and it builds and run the thing, in sublime or in a terminal, but just so that I type in input.

0 Likes

#6

If your build system used the Terminus package, then invoking the build would invoke Terminus to run the build which would result in a terminal tab appearing right inside of Sublime to run your program, which you could interact with.

The rule of thumb is that if you can construct a command line in the terminal that does a thing for you, then you can probably make a sublime-build file do the same thing, since it’s just running some external program with some series of arguments, which is exactly what you’d be doing in the terminal anyway.

A stumbling block to achieving this is that in order to tell Sublime how to run the build you need to know how to do what you want in a terminal already. Java is particularly notorious in this regard just because it’s incredibly sensitive to filenames and file layout (every class has to be in a file with the right name, the locations of the files have to match their locations in the package they’re in, etc).

If you can determine what command you need to run in the Terminal to do this, we can help you set up a build system in Terminus that would do it for you.

0 Likes

#7

[SOLUTION]

I have recently found the solution suiting to my needs, so here’s a step by step :

  1. Install Package Control if you haven’t already
  2. Install Terminus
    2.1. To do so open the palette, press ctrl + shift + p (Win, Linux) or cmd + shift + p (Mac)
    2.2 Type in Package Controll: InstalThis text will be hiddenl Package then select with enter
    2.3 Type in Terminus then select with enter
  3. In the top menu bar go to Tools > Build Systems > New Build System
  4. It should have opened a new file untitled.sublime-build, erase all its content and paste :
    {
    “target”: “terminus_exec”,
    “cancel”: “terminus_cancel_build”,
    “shell_cmd”: “javac $file && java $file_base_name”
    }
  5. Save this file under any name like : RunJava.sublime-build
  6. Now to compile and execute you just have to go to the top menu bar again, go to Tools > Build Systems, and select the build system RunJava. Then with cmd+B (mac) or crtl+B (win) it should open a console within Sublime Text 3 where the result of your code is displayed, and best of all, u can use input with this console !
  7. Celebrate !
1 Like

#8

hey cant really get this working in linux any ideas how to do this in linux ?

0 Likes

#9

Thank you for the solution, but I got an error Error: Could not find or load main class

And it was solved after adding another line

"working_dir": "$file_path"

and the whole thing is :

{
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"shell_cmd": "javac $file && java $file_base_name",
"working_dir": "$file_path"
}
1 Like