Sublime Forum

Using a text file as input for Java

#1

Hello, im trying to learn how to use sublime text and usually I use a text file as my inputs after compiling my Java program. For example, in terminal (UNIX) I would use “java HelloWorld < helloworld.in” where helloworld.in is a text file. I am trying to do this in sublime but I cant seem to figure out how to do so. Thank you!

0 Likes

#2

Sublime Text can execute any arbitrary program you want via a build system. If you use shell_cmd in your build, then Sublime will execute that string as if you typed it manually into a terminal.

As an example, for a simple single-file Java programs you could use a build system such as this:

{
    "shell_cmd": "javac \"$file\" && java $file_base_name  < $file_base_name.in",
    "working_dir": "$file_path",

    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.java"
}

Here the shell_cmd compiles the current .java file and then executes it, using input redirection to send a file in (here the file is named the same as the java file, but with a .in extension).

To use something like this you’d use Tools > Build System > New Build System from the menu and replace the stub with the above (modifiying as needed) and save it as a sublime-build file in the location Sublime will default to, which should be your User package.

The selector will cause it to be selected automatically if the current file is a Java file.

The link above is for the official documentation for build system. In addition, this video series on build systems walks you through them from the basic to the advanced.

0 Likes

#3

Thanks OdatNurd, is there a way for me to build the system to be able to check a different input file name. For example, I am provided with “helloworld1.in”, “helloworld2.in”, “helloworld3.in”. Instead of renaming to the same file name everytime I want to run the build. Thank you.

0 Likes

#4

Depending on how many files there might be you could potentially use a variant in your build to be able to specify different file name iterations; in such a case you would use Tools > Build With... or the associated binding to get Sublime to prompt you for what build to use.

Apart from that, you’d need a plugin of some sort, say one that prompts you for the name of the file before the build runs. However in a case like that you’re not saving yourself a lot of work from just running the command yourself manually.

0 Likes

#5

I dont mind running it manually, I have installed the terminal package on sublime and tried to run my command but was unable to do so. Do you have any suggestions on how to run it manually?

This is my error that i encountered, I am new to programming as well so sorry if this is basic.

0 Likes

#6

I don’t use PowerShell but what I know of it is that it doesn’t work the same as the standard command shell in a variety of ways, so I can’t advise anything here other than to either Google to see how you do input redirection in PowerShell or use cmd in Terminus instead.

0 Likes