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:
An unpleasant black cmd window pops up every time
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:
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:
In regedit I wrote simple:
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.