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

This topic is 3393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
You could also create a jar from those class file(s). Jars are typically executable in Explorer, as long as the Manifest specifies the Main-Class.