Jump to content
  • Advertisement
Sign in to follow this  
delta user

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

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!