Sublime Forum

How to run Java code after compiling?

#1

It took me a while of reading through various threads on here & elsewhere, & then tweaking various preferences/setting paths for Sublime Text 2 to pick up the JDK. I tested a file already on my computer, & finally had success compiling it, but now I’m stuck at figuring out how to run the code from there.

I’m using TextPad in my programming class, & need something at home to work on projects if I can’t make it to campus (Citrix is being so kind lately & refuses to open the one application I actually need to make use of right now).

So how do I run the code once I’ve compiled it?
The build of Sublime Text I have installed right now is 2023, & I’m using JDK 6 update 23.

0 Likes

Compiling and Running Java
Questions from a new user
#2

save this as javac + exec.sublime-build in your sublime text packages folder

build javac “$FileName” && java “$BaseName”
lineNumberRegex ^(…?):([0-9]):?([0-9]*)
showWhenFinished false
workingDir $ProjectDir

then select this build in tools, build system. It will now build and run the program when you press F7

0 Likes

#3

Hi,

how about compiling ans running java with ST2?

The javaC file contains:
{
“cmd”: “javac”, “$file”],
“file_regex”: “^(…?):([0-9]):?([0-9]*)”,
“selector”: “source.java”
}

I would also like it to run, but don’t know what to change

0 Likes

#4

I think your best option is to make a batch file or shell script (depending on what platform your on), and then making a .sublime-build file that runs that script

0 Likes

#5

Well, I’ve made the jump to sublime text 2, discovered that the build system is slightly weird now, cant say I like it much as it adds little but seems to take away the option of custom commands to some extent. Maybe I just don’t understand it, and there’s no docs. Whatever. Now I have a bat file in my java/bin dir with the following

@ECHO OFF
cd %~dp1
javac %~nx1
java %~n1

And I have modified the javac build system to use this. works on windows only, but you can do the same thing in sh

0 Likes

#6

What exactly have you changed? I would really like to run java too.

0 Likes

#7

[quote=“aloje”]
What exactly have you changed? I would really like to run java too.[/quote]

Make the bat file with the following, and save it anywhere in your PATH. I suggest C:\Program Files\Java\jdk*\bin\ to keep everything together.

@ECHO OFF
cd %~dp1
javac %~nx1
java %~n1

then edit [sublimetext packages folder]\Java\JavaC.sublime-build

the contents will be

{
	"cmd": "javac", "$file"],
	"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
	"selector": "source.java"
}

replace “javac” with the name of your bat file (for instance, javacexec.bat) and save it.

Tada! now java will compile and run when you press F7

0 Likes

#8

While no one has posted in this thread for a few months, I, too, have this question. I am running ST2 (Build 2080) on MacOSX. The default JavaC build script works fine for me at creating a .class file; however, I would like some way to run the program from within ST2 without having to open my terminal. I have read through all of the “solutions” posted on the forum for running java; however, none of them worked or were only for windows. Has there been any progress toward making a definitive solution? Perhaps a working build script that runs the “java” command or some type of plugin? Thanks for any help!

0 Likes

#9

Still don’t have a solution to this for OSX. Does anyone know a way (a plugin maybe) to run Java after compiling? I would prefer not having to switch to my terminal every time. I know about wbond’s Terminal plugin but I would like a simpler solution…

0 Likes

#10

My solution for this problem:

Java.sublime-build

{ "cmd": "javacr.bat", "$file_base_name" ], "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "working_dir": "${file_path}", "selector": "source.java" }

javacr.bat in PATH(I place it in jdk folder):

@ECHO OFF javac %1.java java %1

For mac or linux you can make same script:

$ cat javacr.sh #!/bin/sh javac $1.java javac $1

don’t forget chmod +x and palce in PATH.

0 Likes

#11

Bat update:

@ECHO OFF javac %1.java if errorlevel 1 goto error echo [OK, running... ] java %1 goto end :error echo [Compile was unsuccessful] goto end :end

0 Likes

#12

Little error in your sh script. you have “javac” twice. The second one should be “java”

should be:

$ cat javacr.sh #!/bin/sh javac $1.java java $1

0 Likes

#13

[quote=“C0D312”]Little error in your sh script. you have “javac” twice. The second one should be “java”

should be:

$ cat javacr.sh #!/bin/sh javac $1.java java $1[/quote]

Thank you, its typos.

Better use this code:

@ECHO OFF javac %1.java if errorlevel 1 goto error echo [OK, running... ] java %1 goto end :error echo [Compile was unsuccessful] goto end :end

Because when you re-compiling your code and have an error, our old java class does not start. It’s better.

0 Likes

#14

[quote=“C0D312”]Little error in your sh script. you have “javac” twice. The second one should be “java”

should be:

$ cat javacr.sh #!/bin/sh javac $1.java java $1[/quote]

hi

In Mac , in which directory i save this code?

Thanks

0 Likes

#15

@elgreen,

save this in your /usr/bin/

0 Likes

#16

Hey guys,

thanks for the tipps…could finally do it =)…I’m on Mac OS X 10.7 running ST2bd2139.

I tried to implement the error handling in the .sh script and I also had to modify it to make it work, don’t know why but the whole $ cat filename thing made it not work and I also had to change the shebang part to /bin/bash instead of /bin/sh…so here’s what I came up with

This is the file javacrun.sh in folder /usr/bin/

#!/bin/bash
echo "Compiling Java sourcecode..."
javac $1.java
if  $? == 0 ]; then { 
	echo "Running the compiled code..."
	java $1
} else {
	echo "Compiling error, no .class file created"
} fi

for this I created a new build system called JavaCRun.sublime-build with the following code inside:

{
	"cmd": "javacrun.sh", "$file_base_name" ],
	"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
	"working_dir": "${file_path}",
	"selector": "source.java"
}

it works so far so good, when the code has no errors it makes the desired .class file and runs it. This works good with “Hello World”-type (very simple) programs but it doesn’t really good with programs which require user input (when I run it in Terminal I get prompted to enter something like a number or so, I can’t do this in the sublime text “console”). The code is compiled successfully and the .class file is created but when the script runs the .class file, it doesn’t work good because I can give the needed user input…

is this a limitation from sublimetext console or is there a way to solve this and allow userinput in sublime text?

thanks for your replys!

0 Likes

#17

Bumping this up, it would be really nice if there was a way to do this.

0 Likes

#18

To answer your question, ST2 does not allow for interactive input. That’s why I find it most convenient to use iTerm’s fullscreen visor (http://www.iterm2.com) plus @wbond’s Terminal plugin to quickly run java programs. I build the file in ST2, then I set iTerm to appear and disappear on a keystroke and run with iTerm. Hopefully ST2 will allow for interactive input in the future but I don’t think it’s top priority atm.

Hope that helps.

0 Likes

#19

Like C0D312, I solve this by using iTerm and @wbond’s terminal plugin. I did make a slight change to the iTerm.sh file within the Terminal plugin. Every time you run it, it opens a new terminal. I didn’t like this so I altered it to just use the current terminal. This way I just build in SB2, cmd + shit + t, and now i’m at the directory in iTerm. From here I just use a text expander snippet to build. It’s not perfect, but just a few keystrokes and its done.

Here is my altered file.

[code]#!/bin/bash

CD_CMD=“cd “\”$(pwd)\”" && clear"
VERSION=$(sw_vers -productVersion)

if (( $(expr $VERSION ‘<’ 10.7) )); then
RUNNING=$(osascript<<END
tell application “System Events”
count(processes whose name is “iTerm”)
end tell
END
)
else
RUNNING=1
fi

if (( $RUNNING )); then
osascript<<END
tell application “iTerm”
activate
set term to (current terminal)
tell term
set sess to (current session)
tell sess
write text “$CD_CMD”
end tell
end tell
end tell
END
else
osascript<<END
tell application “iTerm”
activate
set sess to the first session of the first terminal
tell sess
write text “$CD_CMD”
end tell
end tell
END
fi[/code]

0 Likes

#20

Sorry to bump and old thread.

This is working fine for me.

But if I want to import another library, when I build and run it fails.

The output says it can’t find the library.

When I run javac from the command line it works fine though. CLASSPATH is configured correctly.

Any ideas?

Have searched around and am failing to come up with any thing.

Cheers

0 Likes