jacquesb
jacquesb

Reputation: 13

Windows 7 how to make explorer open a file with a vbs script

I am running openjdk on windows 7 without admin rights

I went through the explorer "open-with" dialogue to select java as the program to "open" the .jar file. To run a .jar file by (double) click, windows executes something like

java (filename).jar

However, java requires the argument -jar, i.e.:

java -jar (filename).jar

To set this up the user needs admin rights to use assoc and ftype,

or implement the register edits as explained in the answer below.

Another workaround is to use a batch file, e.g. javastart.bat:

Listing javastart.jar

start java -jar %1

After going through the explorer "open with" dialogue, this works. Clicking the jar file will open the command window and this will start java.

However, while java is running, the command-window is also open, which is ugly.

Edit

javaw.exe must be called, and the command-windows will close:

start javaw.exe -jar %1

the following script is not needed to close the command window

End of edit

To resolve this, I start a vbs script.

New listing javastart.jar

start startjar.vbs %1

And startjar.vbs:

Set args = Wscript.Arguments
cmd = "java -jar " & chr(34) & args(0) & chr(34)
Set WshShell = CreateObject("WScript.Shell")
CreateObject("Wscript.Shell").Run cmd, 0, True

This works: now I get a short flash of the command window starting the vbs, and then the windowless vbs-script starts java and the jar-file.

However, when I directly open the jar file with startjar.vbs, (right click the jar file, than go through the open with dialogue), the name of the jar file is not passed as an argument to the vbs, but windows tries to run the jar file directly (and gives an error: "the .jar file is not a valid win32-application").

Why is windows explorer not sending the filename as an argument to the vbs script?

Upvotes: 0

Views: 391

Answers (1)

CatCat
CatCat

Reputation: 491

Setting under HKCR come from both HKCU\Software\Classes and HKLM\Software\Classes. You can certainly edit HKCU settings as they are yours. If they exist they override the exact same HKLM settings. Under HKCR you will see a merged view of both with CU overriding any LM settings.

This started off as notepad's settings. Fix the path to your javaw.exe file.

jar files are now on the New menu, are searched by windows search, treated as text files so edit on right click menu but open as a program file.

If you are a non-admin use the reg command to merge (after fixing the path). If you are an admin double-click it.

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Classes\jarfile]
@="Java Program File"

[HKEY_CURRENT_USER\SOFTWARE\Classes\jarfile\shell]

[HKEY_CURRENT_USER\SOFTWARE\Classes\jarfile\shell\open]

[HKEY_CURRENT_USER\SOFTWARE\Classes\jarfile\shell\open\command]
@="C:\\Folder\\javaw.exe -jar \"%1\""

[HKEY_CURRENT_USER\SOFTWARE\Classes\.jar]
@="jarfile"
"Content Type"="text/plain"
"PerceivedType"="text"

[HKEY_CURRENT_USER\SOFTWARE\Classes\.jar\PersistentHandler]
@="{5e941d80-bf96-11cd-b579-08002b30bfeb}"

[HKEY_CURRENT_USER\SOFTWARE\Classes\.jar\ShellNew]
"ItemName"=hex(2):6a,00,61,00,72,00,66,00,69,00,6c,00,65,00,00,00
"NullFile"=""

Upvotes: 1

Related Questions