Issue2346

classification
Title: Launcher (bin/jython.exe, bin/jython) is not resilient to bad environment variable settings
Type: behaviour Severity: normal
Components: Core Versions: Jython 2.7
Milestone: Jython 2.7.2
process
Status: pending Resolution: fixed
Dependencies: Superseder:
Assigned To: jeff.allen Nosy List: adamburke, brettcannon, jeff.allen, stefan.richthofer, zyasoft
Priority: high Keywords:

Created on 2015-04-29.06:22:47 by adamburke, last changed 2018-11-04.14:32:35 by jeff.allen.

Messages
msg9983 (view) Author: Adam Burke (adamburke) Date: 2015-04-29.06:27:38
Jython.exe didn't work for me after install with Jython 2.7.0. Installer seemed to run fine. It looks like there are stray references to a specific machine path z drive? Is jython.exe supposed to be in the build, or not supposed to be invoked directly? 

This is the "final soft launch" 2.7.0 version.

java -jar jython.jar seemed to work ok

--
Windows 7 Enterprise SP1

C:\Working\tools\jython2.7.0\bin>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 1AA0-CF05

 Directory of C:\Working\tools\jython2.7.0\bin

04/29/2015  01:50 PM    <DIR>          .
04/29/2015  01:50 PM    <DIR>          ..
04/29/2015  02:25 AM         1,236,153 jython.exe
04/29/2015  02:25 AM         2,459,136 python27.dll
               2 File(s)      3,695,289 bytes
               2 Dir(s)  105,643,765,760 bytes free

C:\Working\tools\jython2.7.0\bin>jython
Traceback (most recent call last):
  File "<string>", line 444, in <module>
  File "<string>", line 435, in main
  File "Z:\jythondev\jython27\src\shell\build\jython\out00-PYZ.pyz\subprocess",
line 522, in call
  File "Z:\jythondev\jython27\src\shell\build\jython\out00-PYZ.pyz\subprocess",
line 710, in __init__
  File "Z:\jythondev\jython27\src\shell\build\jython\out00-PYZ.pyz\subprocess",
line 958, in _execute_child
WindowsError: [Error 2] The system cannot find the file specified

C:\Working\tools\jython2.7.0\bin>



C:\Working\tools\jython2.7.0>java -jar jython.jar Lib\test\testall.py
Traceback (most recent call last):
  File "Lib\test\testall.py", line 8, in <module>
    import sys, regrtest
  File "C:\Working\tools\jython2.7.0\Lib\test\regrtest.py", line 1344, in <modul
e>
    if ' ' in sys.executable:
TypeError: 'NoneType' object is not iterable

C:\Working\tools\jython2.7.0>java -jar jython.jar
Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_65
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello'
hello
>>> def fib(n):
...     if n == 1:
...
...
...       return 1
...     return n+fib(n-1)
...
>>> fib(3)
6
>>> fib(7)
28
>>>
>>>
C:\Working\tools\jython2.7.0>
msg9984 (view) Author: Jim Baker (zyasoft) Date: 2015-04-29.13:21:28
The Z: drive referred here is my home directory mapped from OSX to Windows 8.1 via VMWare, since I built jython.exe on my OSX laptop. So that's how we go from ~/jythondev/jython27 to Z:\jythondev\jython27

The reported error "WindowsError: [Error 2] The system cannot find the file specified" suggests that it cannot find the Java executable.

Can you try running

jython.exe --print

and report back its results? This will tell you the exact command line the launcher was trying to run.
msg9985 (view) Author: Jim Baker (zyasoft) Date: 2015-04-29.14:59:32
So the relevant line of code is the subprocess.call in https://github.com/jythontools/jython/blob/master/src/shell/jython.py#L435:

            try:
                result = subprocess.call(command)
                if args.help:
                    print_help()
            except KeyboardInterrupt:
                pass

This should be more robust, catching any runtime error and re-wrapping. One possibility is for it to attempt diagnosis: is java/jdb available? Were there other issues that were raised?

Clearly you have Java installed somewhere to do the installation, but you may have to fix up with JAVA_HOME in this particular case.
msg9986 (view) Author: Jim Baker (zyasoft) Date: 2015-04-29.16:58:26
Tried on Windows, setting JAVA_HOME to a bad setting:

c:\jython2.7.0>bin\jython.exe -c "print 47"
47

c:\jython2.7.0>set JAVA_HOME=c:\foo

c:\jython2.7.0>bin\jython.exe -c "print 47"
Traceback (most recent call last):
  File "<string>", line 444, in <module>
  File "<string>", line 435, in main
  File "Z:\jythondev\jython27\src\shell\build\jython\out00-PYZ.pyz\subprocess", line 522, in call
  File "Z:\jythondev\jython27\src\shell\build\jython\out00-PYZ.pyz\subprocess", line 710, in __init__
  File "Z:\jythondev\jython27\src\shell\build\jython\out00-PYZ.pyz\subprocess", line 958, in _execute_child
WindowsError: [Error 2] The system cannot find the file specified

But note that java itself is more resilient if JAVA_HOME is bad:

c:\jython2.7.0>java -jar jython.jar
Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_40
Type "help", "copyright", "credits" or "license" for more information.
>>>

Also here's the output of jython --print:

c:\jython2.7.0>bin\jython.exe --print
c:\foo\bin\java -Xmx512m -Xss1024k -classpath c:\jython2.7.0\jython.jar;. -Dpython.home=c:\jython2.7.0 -Dpython.executable=c:\jython2.7.0\bin\jython.exe -Dpython.launcher.uname=windows -Dpython.launcher.tty=true org.python.util.jython
msg9987 (view) Author: Jim Baker (zyasoft) Date: 2015-04-29.17:00:36
Conclusion: we can make jython.py - and the resulting jython.exe - more resilient if bad environment variables are passed in. However, this seems like a goal for 2.7.1 as we get more experience with what happens. Otherwise, this seems to be a release notes issue for 2.7.0
msg9989 (view) Author: Jim Baker (zyasoft) Date: 2015-04-29.20:10:41
Also impacts other operating systems, in this case OS X 10.10:

$ export JAVA_HOME=/foo
$ bin/jython
Traceback (most recent call last):
  File "bin/jython", line 444, in <module>
    main(sys.argv)
  File "bin/jython", line 431, in main
    os.execvp(command[0], command[1:])
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 346, in execvp
    _execvpe(file, args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 370, in _execvpe
    func(file, *argrest)
OSError: [Errno 2] No such file or directory
msg9990 (view) Author: Adam Burke (adamburke) Date: 2015-04-30.01:02:05
Yes it does seem to be caused by a JAVA_HOME set inconsistently with the Java in the PATH and used for installation.

C:\Working\tools\jython2.7.0>echo %JAVA_HOME%
C:\Program Files (x86)\Java\jdk1.6.0_34

C:\Working\tools\jython2.7.0>bin\jython
Traceback (most recent call last):
  File "<string>", line 444, in <module>
  File "<string>", line 435, in main
  File "Z:\jythondev\jython27\src\shell\build\jython\out00-PYZ.pyz\subprocess",
line 522, in call
  File "Z:\jythondev\jython27\src\shell\build\jython\out00-PYZ.pyz\subprocess",
line 710, in __init__
  File "Z:\jythondev\jython27\src\shell\build\jython\out00-PYZ.pyz\subprocess",
line 958, in _execute_child
WindowsError: [Error 2] The system cannot find the file specified

C:\Working\tools\jython2.7.0>java -version
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

C:\Working\tools\jython2.7.0>set JAVA_HOME=C:\Program Files\Java\jre7

C:\Working\tools\jython2.7.0>bin\jython
Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_65
Type "help", "copyright", "credits" or "license" for more information.
>>> def fib(n):
...     if n <= 1:
...         return n
...     return fib(n-1)+fib(n-2)
...
>>> fib(5)
5
>>> fib(6)
8
>>> fib(8)
21
>>>
C:\Working\tools\jython2.7.0>
msg9991 (view) Author: Jim Baker (zyasoft) Date: 2015-04-30.01:05:13
Cool, we will document in the release notes (on the Jython wiki! thanks for helping us with that!), then see what can be done to provide better diagnosis in 2.7.1. Thanks again for testing!
msg9992 (view) Author: Adam Burke (adamburke) Date: 2015-04-30.01:08:23
For completeness, including the jython --print output

C:\Working\tools\jython2.7.0>echo %JAVA_HOME%
C:\Program Files (x86)\Java\jdk1.6.0_34

C:\Working\tools\jython2.7.0>bin\jython --print
"C:\Program Files (x86)\Java\jdk1.6.0_34\bin\java" -Xmx512m -Xss1024k -classpath
 C:\Working\tools\jython2.7.0\jython.jar;c:\tibco\tibcosoa\tibrv\8.3\lib\tibrvna
tive.jar; -Dpython.home=C:\Working\tools\jython2.7.0 -Dpython.executable=C:\Work
ing\tools\jython2.7.0\bin\jython.exe -Dpython.launcher.uname=windows -Dpython.la
uncher.tty=true org.python.util.jython

C:\Working\tools\jython2.7.0>bin\jython
Traceback (most recent call last):
  File "<string>", line 444, in <module>
  File "<string>", line 435, in main
  File "Z:\jythondev\jython27\src\shell\build\jython\out00-PYZ.pyz\subprocess",
line 522, in call
  File "Z:\jythondev\jython27\src\shell\build\jython\out00-PYZ.pyz\subprocess",
line 710, in __init__
  File "Z:\jythondev\jython27\src\shell\build\jython\out00-PYZ.pyz\subprocess",
line 958, in _execute_child
WindowsError: [Error 2] The system cannot find the file specified

C:\Working\tools\jython2.7.0>
msg10083 (view) Author: Jim Baker (zyasoft) Date: 2015-05-27.20:14:49
Also, Jython 2.7 requires Java 7, so setting JAVA_HOME to
C:\Program Files (x86)\Java\jdk1.6.0_34 is not going to work.

We could still provide a better error message however.
msg10229 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2015-09-04.11:45:29
Note that this issue also occurs, if JAVA_HOME is not set at all, which is the default situation (!) after "just installing Java".

So I would expect it to be a very common error and I am actually surprised that we don't get much more complaints on this.
In combination with the almost useless error-message this is a really a must-fix for 2.7.1.
msg10353 (view) Author: Brett Cannon (brettcannon) Date: 2015-10-14.21:07:24
I was trying to run Jython against the hg.python.org/benchmarks on Windows 10 and I was getting the error reported in this issue. The solution I found had nothing to do with JAVA_HOME but from the fact that PATH and TEMP were not set in the environment. Once I had those two -- plus the usual SystemRoot and COMSPEC -- then things started working again.
msg11727 (view) Author: Jeff Allen (jeff.allen) Date: 2018-02-28.23:21:35
Much work was done on the launcher since 2.7.0: the spurious directory prefixes are gone, and JAVA_HOME not set at all is dealt with. But when JAVA_HOME is invalid, the message is no better:

PS jython-trunk> $env:JAVA_HOME="c:\foo"
PS jython-trunk> dist\bin\jython  -c "print 42"
Traceback (most recent call last):
  File "jython.py", line 540, in <module>
  File "jython.py", line 531, in main
  File "subprocess.py", line 168, in call
  File "subprocess.py", line 390, in __init__
  File "subprocess.py", line 640, in _execute_child
WindowsError: [Error 2] The system cannot find the file specified
[15152] Failed to execute script jython

The uninformative message is the fault of CPython subprocess.call(). I suppose one could catch the exception and print what failed (the same thing as --print).

I take "In combination with the almost useless error-message this is a really a must-fix for 2.7.1" to be a vote for this in 2.7.2.
msg12169 (view) Author: Jeff Allen (jeff.allen) Date: 2018-11-03.23:50:21
I've wrapped the whole final part of the launcher to catch OSError, so now I get (on stderr):

PS jython-trunk> python .\dist\bin\jython.py -c "print 42"
Failed to launch Jython using command c:\foo\bin\java ...
    Use the --print option to see the command in full.
    Launcher used JAVA_HOME = c:\foo
[Error 2] The system cannot find the file specified

and non-zero exit status. Undefining TEMP does not seem to upset Jython. If you define JAVA_HOME sensibly, and undefine PATH, it runs, and if both are blank you get:

PS jython-trunk> $env:JAVA_HOME=""
PS jython-trunk> $env:Path=""
PS jython-trunk> C:\Python\2.7.15\python.exe .\dist\bin\jython.py -c "print 42"
Failed to launch Jython using command java ...
    Use the --print option to see the command in full.
    Check PATH for java command.
[Error 2] The system cannot find the file specified

This ought to be clear enough. I have still to compile this to an exe for check-in.
msg12171 (view) Author: Jeff Allen (jeff.allen) Date: 2018-11-04.14:32:35
This has wandered a bit in what it has tried to cover, but I'm claiming success in the sensible error messages produced by https://hg.python.org/jython/rev/6b10ade8331e .
History
Date User Action Args
2018-11-04 14:32:35jeff.allensetstatus: open -> pending
resolution: accepted -> fixed
messages: + msg12171
2018-11-03 23:50:21jeff.allensetassignee: jeff.allen
type: behaviour
messages: + msg12169
components: - Installer
2018-02-28 23:21:36jeff.allensetpriority: high
nosy: + jeff.allen
messages: + msg11727
2015-12-29 23:45:02zyasoftsetmilestone: Jython 2.7.1 -> Jython 2.7.2
2015-10-14 21:07:25brettcannonsetnosy: + brettcannon
messages: + msg10353
2015-09-04 11:45:29stefan.richthofersetmessages: + msg10229
2015-05-27 20:14:49zyasoftsetmessages: + msg10083
2015-05-12 16:28:37stefan.richthofersetnosy: + stefan.richthofer
2015-04-30 01:08:24adamburkesetmessages: + msg9992
2015-04-30 01:05:30zyasoftsetresolution: accepted
2015-04-30 01:05:13zyasoftsetmessages: + msg9991
2015-04-30 01:02:06adamburkesetmessages: + msg9990
2015-04-29 20:10:41zyasoftsetmessages: + msg9989
title: Windows launcher (jython.exe) is not resilient to bad environment variable settings -> Launcher (bin/jython.exe, bin/jython) is not resilient to bad environment variable settings
2015-04-29 17:01:18zyasoftsettitle: jython.exe -> Windows launcher (jython.exe) is not resilient to bad environment variable settings
2015-04-29 17:00:36zyasoftsetmessages: + msg9987
milestone: Jython 2.7.1
2015-04-29 16:58:26zyasoftsetmessages: + msg9986
2015-04-29 14:59:39zyasoftsetmilestone: Jython 2.7.0 -> (no value)
2015-04-29 14:59:33zyasoftsetmessages: + msg9985
2015-04-29 13:21:29zyasoftsetnosy: + zyasoft
messages: + msg9984
2015-04-29 06:27:39adamburkesetversions: + Jython 2.7
messages: + msg9983
components: + Core, Installer
milestone: Jython 2.7.0
2015-04-29 06:22:56adamburkesettitle: jythondev -> jython.exe
2015-04-29 06:22:47adamburkecreate