2

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?

Jake Symons
  • 468
  • 2
  • 10
  • 19

2 Answers2

0

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
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • So PowerShell learning here I go xD, thanks for guiding, shame there isn't a simpler way. I will post complete ps1 script – Kacper Lechowicz Jul 27 '18 at 09:12
  • 1
    @KacperLechowicz I am not saying that you *must* do it that way. I just anticipate that, in the end, this is a more reliable approach, costing you less time overall ;-) – GhostCat Jul 27 '18 at 09:25
0

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"
  • ok problem solved in command value should be: **powershell "& "E:\Projekty\Java\Resize_Program\out\artifacts\Resize_Program_jar\run.ps1""" "%1"** – Kacper Lechowicz Jul 28 '18 at 23:12