Sublime Forum

How to run Java code after compiling?

#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

#21

If I may ask what does your java build file look like?

I have been trying to get very very simple java programs to run and show their output in the SL console window without success.

Thanks…Dave

0 Likes

#22

[quote=“CyberWalrus”]

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[/quote]

In case anyone new is wondering how to compile and run a .java file from inside Sublime Text, these directions worked for me.
Make sure you edit your PATH variable appropriately. If you don’t know how to do that, just say so and I will make a quick guide, it’s not hard.

0 Likes

#23

Actually, I’m having some trouble now, if there’s anyone out there who can help.

The above was working for me what I was using very very simple java programs (I’m just starting to learn, so it was working for the first lesson).
So, when I had my main class with only a method that prints stuff, it was working.
System.out.print(“Hello World”);
for example.

But now I’m trying something a bit more complicated, and it’s not working. And I know it should be working, because it’s working from cmd (I’m using win7 64 bit in case that’s important)
Here’s the code:

[code]import java.util.Scanner;

public class Addition
{
//main
public static void main( String] args)
{
Scanner input = new Scanner( System.in );

  int number1;
  int number2;
  int sum;

  System.out.print("Adding numbers --");

  System.out.print("Enter first integer: ");
  number1 = input.nextInt();

  System.out.print("Enter second integer: ");
  number2 = input.nextInt();

  sum = number1 + number2;

  System.out.printf( "Sum is %d\n", sum );

}
}[/code]

And here is the error message when I press f7:

Adding numbers -- Enter first integer: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at Addition.main(Addition.java:19) [Finished in 1.1s with exit code 1]

So, looks like it’s having trouble with importing Scanner input or something…dunno.
Anybody have an idea of what’s going on and how to fix it?

For the record, it’s apparently building it correctly, just not running it correctly.

0 Likes