Sign in to follow this  
delta user

[java] How to run a Java class file in Windows Explorer

Recommended Posts

Hello Maybe I can share some IP with you guys around here to make your life a little bit easier. Did you ever wanted to run a java application by clicking on the main class file in Windows Explorer. Well I did. And guess what, it didn't work. java couldn't find the main class. After some research I found out that you need to do some command line tricks to get it working, so I wrote a little batch file to launch a java .class file. Here it is: javaClassLauncher.bat
@echo off
rem *******************************************
rem * DISCLAIMER                              *
rem *   I am not responsible for any damage   *
rem *   to your system or loss of data by     *
rem *   running this application.             *
rem *   Use at your own risk.                 *
rem *                                         *
rem * Note:                                   *
rem *   This will only work if the class      *
rem *   file has a main method in it.         *
rem *   The class file should also not be     *
rem *   included in a package. Other class    *
rem *   files may be included in a package.   *
rem *******************************************

rem - replace "javaw" with "java" if javaw is an unknown command
set command=javaw -cp

rem - check file extention for java class ".class" files
for %%i in (%1) do set filepath=%%~xi
if not "%filepath%" == ".class" goto error

rem - get the directory of the class file
for %%i in (%1) do set filepath=%%~dpi
set filepath=%filepath:~0,-1%
set command=%command% "%filepath%"

rem - get the class file name
for %%i in (%1) do set command=%command% %%~ni

shift

rem - process arguments
:processarg
if "%1" == "" goto launch
set command=%command% %1
shift
goto processarg

rem - run the command
:launch
echo %command%
%command%
goto stop

:error
echo Wrong file type. Must be ".class"
echo.
pause
:stop

Just add the batch file to your file associations for the "class" extension and everything should work fine. Open for comments, suggestions, improvements,... :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this