views:

389

answers:

5

If there's one thing that annoys me about Java it's that you can't double-click a class file so as to run. I assuming there's an entry in the registry that has to be edited to do this but I haven't a clue.

So, as it says on the tin. Does anyone know how to associate Java class files to run on double-click on Windows (I aiming for Windows 7 here but I'm sure there'd be no difference in the three most latest releases)? It would make my life (and I'm sure many other people's) much easier!

Udpate: I've seen answers relating to making a JAR out of the class in question and running it that way. However useful, that is not exactly what I'm looking for here. I'm effectively looking for Windows itself to invoke java with the class on double-click, with the correct arguments.

+2  A: 

When you install the Java Runtime Environment, it registers .jar files as an association in Windows. If you double-click on a .jar file, it will open it using Java. For this to work, you need to make sure you have a manifest defined that points to the class to run. Your class file to be run must have a main method that will be called.

Let's assume you have a class named 'com.TheClass.class' on disk. If you want to have this able to run with double click, create a file in a new directory called META-INF/manifest.mf. Put this into it:

Manifest-Version: 1.2
Main-Class: com.TheClass

Zip (or use the jar command) both your class up with this manifest directory and file. Rename it to mine.jar. Double click on it and it should launch your class with the Java runtime.

Chris Dail
+2  A: 

For a .class file to run, needs in first place to have "something" to do, that is, that .class should contain a main method. Not all the .class do have one.

One thing you can do, is to wrap your app ( a number of .class files ) inside a jar file.

For short, you just need in addition to your classes a manifest file that says, where the main method is:

jar -cmf yourmanifestfile.mf  doubleClickApp.jar  *.class 

And that's it, the doubleClickApp.jar is now executable with a "doubleClick" gesture.

OscarRyz
+3  A: 

The double-clickable JAR solution is the most common plain Java distribution method. There'd be a number of issues with trying to execute .class files directly, with the classpath the one that pops first to mind.

That said, if you wanted to support the very simplest possibilities in your development environment, you could conceivably implement a script that

  • inspected the .class file for the full class name (including package and inner class name)
  • walked up the directory tree to the root of the file's class path
  • (optionally included any common lib directories in the classpath)
  • invoked Java for the determined class

Then you could register your shiny script as a handler for .class files. But since you're in the development environment, aren't you happier with your IDE doing that?

Michael Brewer-Davis
Certainly, I love my IDE doing that for me. But I feel things would be easier if I could click on just any class and ideally a script doing that for me, and invoking java.exe on the appropriate class. I'm assuming a VB script could be associated to the context menu and do that?
Beau Martínez
Sure, VBScript, Java program, batch file. Just use "Open with..." in the context menu to set up your file association (or however you like to do it).
Michael Brewer-Davis
+1  A: 

if classpath doesnt matter too much, easily done with a simple batch file runjava.bat or so that is associated with .class files in the explorer (via right click >> open with..)

@echo off
REM change to folder where the class file resides
cd %~d1%~p1
REM execute the class by calling its name without file extension
start java %~n1

cheers

pete
A: 

easier way i found was to create a shortcut on the same folder as the .class file. then right click on it and go to properties. change the field Target to "java NameOfClass" without quotes then double click the shortcut :)

bianca