# How to run Java code after compiling?

**URL:** <https://forum.sublimetext.com/t/how-to-run-java-code-after-compiling/1295>\
**Category:** Technical Support\
**Created:** [February 18, 2011, 2:36pm UTC](https://forum.sublimetext.com/t/how-to-run-java-code-after-compiling/1295 "2011-02-18T14:36:58Z")\
**Posts on this page:** 1\
**Showing post:** 16

<div class="post-metadata">

**Author:** ![lluuuk](https://forum.sublimetext.com/letter_avatar_proxy/v4/letter/l/ad7895/32.png) [@lluuuk](https://forum.sublimetext.com/u/lluuuk)\
**Post date:** [November 4, 2011, 2:24pm UTC](https://forum.sublimetext.com/t/how-to-run-java-code-after-compiling/1295/16 "2011-11-04T14:24:05Z")

</div>

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/

```auto
#!/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:

```auto
{
	"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!

---

_[View the full topic](https://forum.sublimetext.com/t/how-to-run-java-code-after-compiling/1295)._
