Sublime Forum

How to run Java code after compiling?

#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