Home Download Buy Blog Forum Support

How to run Java code after compiling?

Re: How to run Java code after compiling?

Postby leonko on Mon Sep 26, 2011 9:20 pm

Bat update:

Code: Select all
@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
leonko
 
Posts: 3
Joined: Mon Sep 26, 2011 8:58 pm

Re: How to run Java code after compiling?

Postby C0D312 on Mon Sep 26, 2011 9:53 pm

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

should be:
Code: Select all
$ cat javacr.sh
#!/bin/sh
javac $1.java
java $1
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: How to run Java code after compiling?

Postby leonko on Tue Sep 27, 2011 10:16 am

C0D312 wrote:Little error in your sh script. you have "javac" twice. The second one should be "java"

should be:
Code: Select all
$ cat javacr.sh
#!/bin/sh
javac $1.java
java $1


Thank you, its typos.

Better use this code:

Code: Select all
@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.
leonko
 
Posts: 3
Joined: Mon Sep 26, 2011 8:58 pm

Re: How to run Java code after compiling?

Postby elgreen on Thu Oct 13, 2011 10:30 am

C0D312 wrote:Little error in your sh script. you have "javac" twice. The second one should be "java"

should be:
Code: Select all
$ cat javacr.sh
#!/bin/sh
javac $1.java
java $1


hi

In Mac , in which directory i save this code?

Thanks
elgreen
 
Posts: 1
Joined: Thu Oct 13, 2011 10:25 am

Re: How to run Java code after compiling?

Postby C0D312 on Thu Oct 13, 2011 7:11 pm

@elgreen,

save this in your /usr/bin/
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: How to run Java code after compiling?

Postby lluuuk on Fri Nov 04, 2011 2:24 pm

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/

Code: Select all
#!/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:

Code: Select all
{
   "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!
lluuuk
 
Posts: 3
Joined: Sun Oct 09, 2011 11:28 pm

Re: How to run Java code after compiling?

Postby tretretre on Sun Nov 06, 2011 10:18 pm

lluuuk wrote: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?


Bumping this up, it would be really nice if there was a way to do this.
tretretre
 
Posts: 23
Joined: Fri Oct 21, 2011 7:13 pm

Re: How to run Java code after compiling?

Postby C0D312 on Sun Nov 06, 2011 10:25 pm

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.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: How to run Java code after compiling?

Postby hatunike on Tue Jul 17, 2012 6:19 pm

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: Select all
#!/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
hatunike
 
Posts: 2
Joined: Tue Jul 17, 2012 6:05 pm

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.

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

PreviousNext

Return to Technical Support

Who is online

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