1

I'm trying to run a .jar file by right-clicking on a folder and selecting an item in the context menu. It works when the (Default) value in HKEY_CLASSES_ROOT\Directory\shell\MyJar\command is:

"C:\Program Files (x86)\Java\jdk1.8.0_40\bin\java.exe" -jar "D:\path\to\MyJar.jar"

But with the following value, I get a popup saying "Application not found":

java -jar "D:\path\to\MyJar.jar"

Either of those lines works just fine if I enter it into the command prompt, so I'm thinking the problem has to do with the registry somehow not "seeing" the classpath path.

Below are my system variable JAVA_HOME and the beginning of my system variable Path, respectively:

C:\Program Files (x86)\Java\jdk1.8.0_40
%JAVA_HOME%\bin;C:\ProgramData\Oracle\Java\javapath;

I'm on windows 8.1, and java version 1.8.0_40.

EDIT: clarified wording. Answer to Guoliang Liu:

1. Yes.

2. java -version yields:

java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b26)  
Java HotSpot(TM) Client VM (build 25.40-b25, mixed mode, sharing)

3. echo %PATH% begins with

C:\Program Files (x86)\Java\jdk1.8.0_40\bin;C:\ProgramData\Oracle\Java\javapath;

echo %JAVA_HOME% yields:

C:\Program Files (x86)\Java\jdk1.8.0_40
H.v.M.
  • 1,348
  • 3
  • 16
  • 42

3 Answers3

1

The problem is when you create a key in the registry editor the key is always created as REG_SZ value for which environment variables are not resolved. So you would need to create a key as REG_EXPAND_SZ. For which the environment variables are resolved. This you can't do in the registry editor.

To achive what you want to do you need

  • create the system environment variable JAVA_HOME
  • create a registry file myjar.reg with the content below

The hex value is "%JAVA_HOME%\bin\java.exe" -jar "D:\path\to\MyJar.jar"

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\MyJar\command]
@=hex(2):22,00,25,00,4a,00,41,00,56,00,41,00,5f,00,48,00,4f,00,4d,00,45,00,\
  25,00,5c,00,62,00,69,00,6e,00,5c,00,6a,00,61,00,76,00,61,00,2e,00,65,00,78,\
  00,65,00,22,00,20,00,2d,00,6a,00,61,00,72,00,20,00,22,00,44,00,3a,00,5c,00,\
  70,00,61,00,74,00,68,00,5c,00,74,00,6f,00,5c,00,4d,00,79,00,4a,00,61,00,72,\
  00,2e,00,6a,00,61,00,72,00,22,00,00,00

Import the myjar.reg into the registry (in the registry editor File -> Import). Then amend the path to your Jar file in the editor.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • It works, but the end result is still pretty much the same as typing out the full path. The goal was to make it work with just `java -jar "D:\path\to\MyJar.jar"`. Are you saying this is not possible? – H.v.M. Mar 30 '15 at 15:58
  • Then you need to change the PATH (system environment variable, not the users one) to include the directory where your `java.exe` is stored. If it's not working please describe detailed which steps you have done. – SubOptimal Mar 31 '15 at 05:40
  • The only thing I've tried that's not described in the OP is making (Default) of the type `REG_EXPAND_SZ` by following your instructions. And as the OP bears out, the java `bin` folder, which contains `java.exe`, is part of PATH. – H.v.M. Mar 31 '15 at 10:30
  • Keep in mind that environment variables are set per user and for the system. If the directory is in the users PATH it will not be found by the system. Simple check: create the `MyJar\command` as `cmd`, so it will open a CMD session with the system environment. Type `set` and check the settings of `JAVA_HOME` and `PATH`. – SubOptimal Mar 31 '15 at 10:36
  • `JAVA_HOME` is as in OP, and `PATH` begins with the `bin` folder as in OP (though here with `JAVA_HOME` resolved). It's not identical though; the `PATH` displayed here is of the form "(system PATH);(user PATH)". – H.v.M. Apr 01 '15 at 12:50
  • FWIW, I have no trouble creating `REG_EXPAND_SZ` values in the registry editor on Windows 7 and Windows 10. It's a simple matter of right-clicking the key and selecting New > Expandable String Value. There is need to import a hand-crafted `.reg` file. – George Mar 28 '19 at 03:38
  • @George It's about to create a new key (not a value) with `New > Key` as `REG_EXPAND_SZ` and not as `REG_SZ`. To get environment variable references expanded (https://learn.microsoft.com/en-us/windows/desktop/sysinfo/registry-value-types). – SubOptimal Jun 05 '19 at 07:25
0

it may help to Check following things.

  1. after you set the environment variables, have you restarted cmd yet?
  2. Is there more than one JDK or JRE? check commond java -version to see whethere you have successfully set it up.
  3. Also see echo %PATH% and %JAVA_HOME% to confirm your settings.

hope it helps.

  • It sure is a PATH problem. My only wonder is `C:\ProgramData\Oracle\Java\javapath;` in variable PATH. I don't use oracle so I am not sure whether that will influence or not. Thought it may worth checking it out. – Gavin The Handsome Mar 27 '15 at 19:32
  • Is Oracle even a program? I think it's just something that came with the 64 bit JRE installation, because all that's in the folder is three .symlink (shortcut) files to java.exe, javaw.exe and javaws.exe in the `C:\Program Files\Java\jre1.8.0_40\bin` folder. Regardless, I tried removing it from PATH and nothing changed. – H.v.M. Mar 27 '15 at 21:33
  • Okay. Now I got nothing to debug this. I would rather try download another jdk. One more thing is that is it possible that PATH is set as user environment variable, but admin does not have it or vice versa. Just guessing. – Gavin The Handsome Mar 27 '15 at 22:52
  • Well, there's one window for "User variables for -username-" and one for "System variables", and PATH and JAVA_HOME are both system variables. – H.v.M. Mar 27 '15 at 23:42
0

There is no CLASSPATH problem here. The problem is java.exe not being found. It is a PATH problem.

user207421
  • 305,947
  • 44
  • 307
  • 483