Share this page 

Execute a class from Windows Explorer (this howto is deprecated)Tag(s): DEPRECATED


This HowTo will you show how to start a class (with a main() method) without opening a DOS Shell or using a JAR file directly from Windows Explorer.

Type the following VBScript using a text editor and save it as WRunJava.vbs.

You need to save the script in the SendTo subdirectory located in the Windows directory

On a Win98 system, it's C:\Windows\SendTo.

On XP, go to Documents and Settings folder then select the folder of a specific user, eg. c:\documents and settings\'user name'\SendTo.

NOTE The SendTo folder is hidden by default. If it is not visible, on the Tools menu, click Folder Options. On the View tab, click Show hidden files and folders. SO you save the scripts shown below, into the SendTo folder.

Then in Window Explorer, locate a class file that can be executed (with a main() method). Right click on it, select the Send To menu and you should see an entry called WRunJava.vbs. Simply select it and the class should be launched via javaw.exe .

' [WRunJava.vbs] start a java class without a console

Dim WSHShell, FSO, javaclass, javacompletepath, cmdline

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")

If WScript.Arguments.Count = 0 Then
   WScript.Echo  "no argument on the command line."
Else
   javaclass = WScript.Arguments(0)
   If fso.GetExtensionName(javaclass) = "class" Then
      javacompletepath = fso.GetAbsolutePathName(javaclass)
      javapath = fso.GetParentFolderName(javacompletepath)
      javaclass = fso.GetBaseName(javaclass)
      cmdline = "javaw.exe -cp " & javapath & " " & javaclass
      WSHShell.Run cmdline, 1, false
   ElseIf fso.GetExtensionName(javaclass) = "jar"  Then
      javacompletepath = fso.GetAbsolutePathName(javaclass)
      cmdline = javaclass
      WSHShell.Run cmdline, 1, false
   Else
      WScript.Echo "Not a java class! (" & javaclass & ")"
   End if
End If

Set WSHShell = Nothing
Set FSO = Nothing
WScript.Quit(0)

You need a second script to launch a java class with a console (useful to see debugging traces or text-only Java program). Called it CRunJava.vbs . Then you will have two choices from the SendTo menu, one for console-based program and one for GUI-based (Windows) program.

' [CRunJava.vbs] start a java class with a console

Dim WSHShell, FSO, javaclass, javacompletepath, cmdline

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")

If WScript.Arguments.Count = 0 Then
   WScript.Echo  "no argument on the command line."
Else
   javaclass = WScript.Arguments(0)
   If fso.GetExtensionName(javaclass) = "class" Then
      javacompletepath = fso.GetAbsolutePathName(javaclass)
      javapath = fso.GetParentFolderName(javacompletepath)
      javaclass = fso.GetBaseName(javaclass)
      ' keep the console open
      cmdline = "cmd /k java.exe -cp " & javapath & " " & javaclass
      ' close the console
      ' cmdline = "cmd /c java.exe -cp " & javapath & " " & javaclass
      WSHShell.Run cmdline, 1, false

   ElseIf fso.GetExtensionName(javaclass) = "jar"  Then
      javacompletepath = fso.GetAbsolutePathName(javaclass)
      cmdline = "java.exe -jar " & javaclass
      WSHShell.Run cmdline, 1, false
   Else
      WScript.Echo "Not a java class! (" & javaclass & ")"
   End if
End If

Set WSHShell = Nothing
Set FSO = Nothing
'WScript.Quit(0)
Note : A JAR can be made self executable (with double click), see this HowTo