|
From: David C. <dav...@gm...> - 2015-11-06 22:45:39
|
Reporting back, as I have found a solution, even if I do not completely
understand it.
The Jython program I was executing in my call to InteractiveConsole.exec()
terminated with a call to sys.exit(0). This causes the call to
InteractiveConsole.exec() to throw an exception, which my Java program did
not catch. (jython.java catches this exception, which is why it works.)
When the Java program terminates, it prints the SystemExit exception, but
apparently leaves the InteractiveConsole running (daemon thread?).
All that was necessary to get my program to terminate properly was to catch
the exception thrown by InteractiveConsole.exec() and pass it to
Py.printException(). At this point, my Java program terminates
immediately. It never returns from Py.printException().
Py.printException() does not throw anything.
This was a little surprising to me for two reasons. The first reason is
that the method has 'print' in its name, so I expected it (just) to print
the exception. The second reason is that jython.java follows its own call
to Py.printException() with additional things:
interp.cleanup();
System.exit(-1);
Perhaps there are conditions in which Py.printException() does *not*
terminate the Java program.
David
On Thu, Nov 5, 2015 at 5:38 PM, David Charles <dav...@gm...>
wrote:
> Greetings Jythonistas,
>
> I have a Jython program that runs and exit normally when I invoke it in
> this way:
>
> $ jython myprogram.py args
>
> But runs to its end and _does_not_exit_ when I embed it inside of a *.jar
> file and invoke it in this way:
>
> $ java -classpath jar1:jar2:jar3 MainClass args
>
> I must send a ctrl-C to stop my program when I run it in this way.
>
> I have embedded the Jython interpreter in the following manner:
>
> // Configure the Jython interpreter.
> Properties preProps = PySystemState.getBaseProperties();
> // preProps.put("python.cachedir.skip", false);
> Properties postProps = new Properties();
> postProps.put("python.cachedir.skip", false);
> postProps.put("python.path", "");
> PySystemState.initialize(preProps, postProps, appArgs);
>
> // Start the Jython interpreter.
> InteractiveConsole con = new InteractiveConsole();
> // I found that for some reason, when running under java web start
> // sys.prefix is None and the Lib directory under the jython.jar
> // is not added to the sys.path automatically.
> con.exec("import sys");
> con.exec("sys.path.append(\"__pyclasspath__/Lib\")");
> con.exec("sys.prefix = \"\"");
>
> // Execute the application 'launch code' on the interpreter.
> con.exec(appLaunchCode);
>
> Other Jython programs, embedded in the same manner, do not exhibit this
> problem. I suspect this application does something a little... different.
> But, why then does my application exit normally when I run it on
> $JYTHON_HOME/bin/jython?
>
> Thanks,
> David
>
>
|