Home Download Buy Blog Forum Support

How to run Java code after compiling?

Re: How to run Java code after compiling?

Postby Jonzi on Fri Nov 30, 2012 1:57 pm

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.

Posts: 1
Joined: Fri Nov 30, 2012 1:56 pm

Re: How to run Java code after compiling?

Postby DaveLG526 on Sat Dec 22, 2012 10:22 pm

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.

Posts: 4
Joined: Tue Jul 10, 2012 4:57 pm

Re: How to run Java code after compiling?

Postby flannel jesus on Wed Jan 09, 2013 8:01 pm

CyberWalrus wrote:
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.
Code: Select all
cd %~dp1
javac %~nx1
java %~n1

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

the contents will be
Code: Select all
   "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

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.
flannel jesus
Posts: 9
Joined: Thu Dec 20, 2012 10:08 pm

Re: How to run Java code after compiling?

Postby flannel jesus on Thu Jan 10, 2013 3:14 pm

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: Select all
import java.util.Scanner;

public class Addition
   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 );

And here is the error message when I press f7:
Code: Select all
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.
flannel jesus
Posts: 9
Joined: Thu Dec 20, 2012 10:08 pm


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot] and 26 guests