Java icon indicating copy to clipboard operation
Java copied to clipboard

Add file association with .jar

Open pigsflew opened this issue 6 years ago • 3 comments

Running executable JARs requires calling java.exe -jar "%1", but when selecting the java.exe in explorer, it uses java.exe "%1", which fails.

i suggest adding something like:

manifests:

{
    [ ... ]
    "postinstall" = [ 'echo "In order to associate jar files, run sudo associate-javaw.bat"' ],
    [ ... ]
}

/scripts/associate-javaw.bat:

@echo off
assoc scoop-javaw="%JAVA_HOME%\bin\javaw.exe" -jar "%1";
assoc .jar=scoop-javaw

pigsflew avatar Nov 21 '19 06:11 pigsflew

I couldn't get this to work in Windows 11, sadly. Here's how I got it to work instead:

I found a program that can set file associations here (I found a scoop manifest for it here). But it needs a ProgId to be passed as the file type association, so I modified your batch file to a registry file like so:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\.jar]
@="scoop-javaw"

[HKEY_CURRENT_USER\Software\Classes\scoop-javaw]
@="Java Archive"

[HKEY_CURRENT_USER\Software\Classes\scoop-javaw\DefaultIcon]
@="C:\\Users\\<username-goes-here>\\scoop\\apps\\openjdk\\current\\bin\\javaw.exe,1"

[HKEY_CURRENT_USER\Software\Classes\scoop-javaw\shell]

[HKEY_CURRENT_USER\Software\Classes\scoop-javaw\shell\open]

[HKEY_CURRENT_USER\Software\Classes\scoop-javaw\shell\open\command]
@="\"C:\\Users\\<username-goes-here>\\scoop\\apps\\openjdk\\current\\bin\\javaw.exe\" -jar \"%1\""

Unfortunately, I couldn't get %JAVA_HOME% to work in the registry, so the full path had to be used (replace the two <username-goes-here> with your username/scoop path if on another drive).

Once you've saved this as a .reg file and run it, then finally you can use SetUserFTA:

> SetUserFTA .jar scoop-javaw

The file association should work immediately. But for the DefaultIcon to show up you might need to restart File Explorer. Hope this helps someone.

Edited 05/09/2022 to fix reg file not adding paths to registry (needed to escape each \ and ")

tech189 avatar Dec 05 '21 22:12 tech189