简体   繁体   中英

How add context menu item to Windows Explorer for folders different than exe

I successfully added an item to the context menu of folders (that's the easy part) but my program is jar extension when I try to open it via context menu error appears: This program will not work on your computer or something like this, so I tried in command value write this:

Java -jar path.jar "%1"

Results were the same. My workaround is bat file to open jar file xD but there are two problems:

  1. An unpleasant black cmd window pops up every time

  2. Paths with spaces don't work (the program is given null?)

How to do it so I can rid of this bat?

I think, this is much more of a Windows than a java problem, so more of a generic approach:

Don't try to directly run your java code. Instead: write a script (for example using PowerShell that gets called).

And within that script, you could be doing things such as:

  • searching the windows machine for a java installation
  • checking/translating relative paths to "correct" absolute values
  • to then, finally invoke some java.exe with correct parameters

Here is my simple code :

param (
[string]$p1
)
function Find-JavaExe {
[CmdletBinding()]
param ()

$JavaExeSuffix = 'bin\java.exe'

if ($env:JAVAHOME -eq $null) {
    Write-Debug "`$env:JAVAHOME doesn't exist, going to look elsewhere"
}
else {
    $JavaHomeBasedPath = Join-Path $env:JAVAHOME $JavaExeSuffix
    Write-Debug "Testing for $JavaHomeBasedPath, based on `$env:JAVAHOME"
    if (Test-Path $JavaHomeBasedPath) {
        Write-Debug "Found $JavaExePath"
        return $JavaExePath
    }
}

$RegistrySearchPaths = @('HKLM:\SOFTWARE\JavaSoft\Java Runtime Environment\', 'HKLM:\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment\')
$JavaExePath = $RegistrySearchPaths |
    where { Test-Path $_ } |
    % {
        $CurrentVersion = (Get-ItemProperty $_).CurrentVersion
        Write-Debug "Current Java version is $CurrentVersion, based on $($_)"
        $VersionKey = Join-Path $_ $CurrentVersion
        $JavaHomeBasedPath = Join-Path (Get-ItemProperty $VersionKey).JavaHome $JavaExeSuffix
        Write-Debug "Testing for $JavaHomeBasedPath, based on $VersionKey\JavaHome"
        if (Test-Path $JavaHomeBasedPath) { $JavaHomeBasedPath }
    } |
    select -First 1

if ($JavaExePath -ne $null) {
    Write-Debug "Found $JavaExePath"
    return $JavaExePath
}

}

$path_java = Find-JavaExe
$java_param =  " -jar "
$path_prog = 
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('.\')
$program_name = "\ResizeProgram.jar "
$command = $path_java + $java_param + $path_prog + $program_name + """$p1"""
iex $command


it works fine and handles paths with spaces
but I have another problem when running it from comtextmenu of the folder, prompt appears saying:

  • This program will not work on your computer; again.

In regedit I wrote simple:

  • path.ps1 "%1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM