Home Download Buy Blog Forum Support

How to run Java code after compiling?

How to run Java code after compiling?

Postby _fish on Fri Feb 18, 2011 2:36 pm

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.
_fish
 
Posts: 1
Joined: Fri Feb 18, 2011 2:21 pm

Re: How to run Java code after compiling?

Postby CyberWalrus on Fri Mar 04, 2011 5:23 pm

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
CyberWalrus
 
Posts: 4
Joined: Fri Mar 04, 2011 5:20 pm

Re: How to run Java code after compiling?

Postby aloje on Sun Mar 06, 2011 12:02 pm

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
aloje
 
Posts: 11
Joined: Sat Feb 26, 2011 1:03 pm

Re: How to run Java code after compiling?

Postby jps on Sun Mar 06, 2011 11:39 pm

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
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Re: How to run Java code after compiling?

Postby CyberWalrus on Sat Mar 12, 2011 9:27 am

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
Code: Select all
@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
CyberWalrus
 
Posts: 4
Joined: Fri Mar 04, 2011 5:20 pm

Re: How to run Java code after compiling?

Postby aloje on Sat Mar 19, 2011 7:30 pm

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


What exactly have you changed? I would really like to run java too.
aloje
 
Posts: 11
Joined: Sat Feb 26, 2011 1:03 pm

Re: How to run Java code after compiling?

Postby CyberWalrus on Sat Mar 19, 2011 11:17 pm

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


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
@ECHO OFF
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
CyberWalrus
 
Posts: 4
Joined: Fri Mar 04, 2011 5:20 pm

Re: How to run Java code after compiling?

Postby C0D312 on Sun Jul 10, 2011 4:49 pm

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

Re: How to run Java code after compiling?

Postby C0D312 on Fri Sep 02, 2011 12:41 am

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

Re: How to run Java code after compiling?

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

My solution for this problem:

Java.sublime-build
Code: Select all
{
   "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):
Code: Select all
@ECHO OFF
javac %1.java
java %1


For mac or linux you can make same script:
Code: Select all
$ cat javacr.sh
#!/bin/sh
javac $1.java
javac $1


don't forget chmod +x and palce in PATH.
leonko
 
Posts: 3
Joined: Mon Sep 26, 2011 8:58 pm

Next

Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 22 guests