Associate Java class-files to run on double-click on Windows
Asked Answered
S

6

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.

Sulphurate answered 9/2, 2010 at 20:11 Comment(6)
what would you expect a .class do if it doesn't have a main method ( that is if it doesn't do anything? )Promptbook
ideally, NoMainMethodException or whatever java should throw when you pass a class without a main method to itSulphurate
you need the .jar mechanism to list what class has the main and where all the dependencies are. there is little usefulness in trying to make a .class file "runnable" since you still need to set up the classpath with dependencies, and there are always dependenciesAntinucleon
Classes reside in packages that map to a directory structure... if you're currently in directory z, and your class is w.x.y.z.ClassName - how are you going to know how far to go up the directory hierarchy to provide the classpath for even the class you're trying to run? So you're stuck with only executing classes in the "default" (aka none specified) package. All these issues are resolved with creating an executable JAR...Oribelle
Did you figure out it how to do this?Liddle
@Liddle Not at all, hence why it's still an open question.Sulphurate
U
6

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
Urochrome answered 15/4, 2010 at 16:36 Comment(1)
And no doubt associate class files with the bat. I like the CD to deal with the classpath. Is the 'start' advantageous? also good to add .class to PATHEXT set PATHEXT=%PATHEXT%;.class so from the command line you can just enter the class filename without the extension of the class. Also you can replace %~d1%~p1 with %~dp1 as mentioned in for /?Darmstadt
V
3

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?

Vada answered 9/2, 2010 at 20:40 Comment(2)
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?Sulphurate
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).Vada
C
2

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.

Confab answered 9/2, 2010 at 20:25 Comment(0)
P
2

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.

Promptbook answered 9/2, 2010 at 20:35 Comment(0)
P
1

http://justaddhotwater.webs.com/javaexec.htm

This software makes it possible to run your Java classes by double-clicking them.(Windows ONLY).

Poised answered 22/7, 2011 at 6:39 Comment(0)
P
0

The easiest way that I have found was creating a shortcut on the same folder than the .class file. Then right click on it and go to properties. Change the field Target to java NameOfClass, finally double click the shortcut :)

Pronouncement answered 2/10, 2010 at 5:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.