Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associate Java class-files to run on double-click on Windows

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.

like image 716
Humphrey Bogart Avatar asked Feb 09 '10 20:02

Humphrey Bogart


2 Answers

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
like image 81
pete Avatar answered Oct 05 '22 11:10

pete


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?

like image 27
Michael Brewer-Davis Avatar answered Oct 05 '22 13:10

Michael Brewer-Davis