Windows Trick: Executable Class Files

This is in "new to java" for a reason.

I dont want to be flamed for posting something obvious, lol.

Its just a simple trick to fire up programs by double

clicking a class file. This is for minor cases that dont warrant an

executable JAR.

I was never able to get this working before but i stumbled on this

tech tip that made it all possible:

http://blogs.msdn.com/ben/archive/2007/03/09/path-manipulation-in-a-batch-file.aspx

HOW TO:

Create a new text file with the following contents:

@title %~n1

cd "%~dp1"

java -cp . %~n1

Alternately, if you dont want to see the command window at all (like

with javaw) use this text INSTEAD:

start "%~n1" /D"%~dp1" javaw -cp . %~n1

Save this file as "javalauncher.bat" (it has to have a .bat extension).

Save it into a reliable folder. The java folder is a good place

for this. On my computer that is: C:\Program Files\Java\jdk1.6.0

FINALLY:

(1) Open Windows Explorer (or "My Computer")

(2) Go to Tools -> Folder Options -> File Types

(3) Find the CLASS extension (or create it by hitting the "New" button)

(4) Select it and hit "Advanced"

(5) Select "open" action and hit "Edit" or create a new action

Action: open

Application Used To Perform Action: C:\Program Files\Java\jdk1.6.0\javalauncher.bat "%1"

Explanation:

The action variable %1 returns a full path. Passing this to java

with "java %1" doesnt work so I gave up on this a long time ago.

Unfortunately the batch file file path manipulation trickdoesnt work

in the action window "java %~f1" so a batch file is necessary.

Also anyone trying to condense these lines:

cd "%~dp1" java -cp . %~n1 into this:

java -cp "%~dp1" %~n1 ... that willNOT work

if you are in a folder with spaces in the path.

For some reason java doesnt recognize quoted

paths that end in a path separator:

java -cp "C:\a space\" Program< fails

Why not a simple exe? With this method you can pop into the java

launcher bat file and make little tweaks if you ever have to.

Plus a SINGLE line of text is better then opening up MSVC++

creating an entire project, compiling etc etc.

Why not a JAR? This is just for the simple programs beginners would

write (an intermediate step between the command line and an IDE!).

Why else is this a good idea? Beginners learn the basics of BAT files

and what they can do. This technique is extensible to a Compile and

Jar Me Up! action.

Also, you can create actions involving Java programs called from a

bat for more complicated things. For example: right click on a .JAVA file

and it will write the manifest file and jar up every java file in the folder.

Also, why not. It takes 2 minutes?

EDIT: quotes missing in the start command fixed

[3011 byte] By [TuringPesta] at [2007-11-27 11:51:25]
# 1

cool, got it to work for myself, thanks for the info. I dont know anything bout .bat files =\

It might be helpful to learn a bit.

Thanks again for your help

smithdale87a at 2007-7-29 18:38:02 > top of Java-index,Java Essentials,New To Java...
# 2

Pretty neat. You should put it on that Sun Tips site.

_helloWorld_a at 2007-7-29 18:38:02 > top of Java-index,Java Essentials,New To Java...
# 3

Wow, im really happy that i got positive feedback!

Ive been thinking about putting up a Java blog/techtip thing

on my website. I might just start with this. Thanks.

TuringPesta at 2007-7-29 18:38:02 > top of Java-index,Java Essentials,New To Java...
# 4

> Wow, im really happy that i got positive feedback!

Well we can't have that.

*Ahem*

Yo post sucks and yo mama is ugly.

Djaunla at 2007-7-29 18:38:02 > top of Java-index,Java Essentials,New To Java...