Using Nailgun in Eclipse for Java and Jython

2019-08-15 08:38发布

问题:

I have just set up Nailgun (https://github.com/martylamb/nailgun, http://martiansoftware.com/nailgun/), which is a clever piece of kit which makes the JVM hang around in memory making Java and Jython (potentially) launch more quickly.

This is great for Java, but it promises to be absolute gold dust for me for Jython, particularly during development (Jython is the best language in the world, but has a slow start-up time).

Googling to see whether anyone had tackled running Nailgun on Eclipse I found a red herring, which is "nailgun/eclipse-gtkrc" at GitHub: "nailgun" is just the guy's handle. I also found a CS dept had in fact tackled this, at http://www.cs.dartmouth.edu/~jygsaw/getting_started.html. They seem actually to have managed getting this working for Jython, which was potentially brilliant, but unfortunately their system relies on using a BASH script (Mac OS), and I'm forced to work on Windows (W7) for the moment.

What I then did was

1) Started up the Nailgun server outside Eclipse, including jython.jar in the CLASSPATH,

2) Then I created a client.bat file consisting of a single line:

ng org.python.util.jython %1

3) Then I set up an "external tool configuration" in Eclipse called "ng_client", as per the above CS dept's instructions, except that I set the Location to the above client.bat file, and I made the Arguments: "${selected_resource_loc}" (<-- including quotes, because unfortunately at the moment I have spaces in this path)

4) I ran this "ng_client" once so it failed, but was then included in this External tools list of tools.

5) I then selected a trivial Jython py file (trivial.py) and went Run --> External tools --> ng_client.

The console showed the following result

C:\Program Files (x86)\eclipse-jee-luna-SR2-win32\eclipse>ng org.python.util.jython "G:\My Documents\software projects\trivial.py"

recv: No error

NB "recv: No error" is in red. The program, I hasten to add, did not run.

Incidentally, for anyone interested, this led to the Nailgun server terminal giving out the following error message:

C:\Users\Mike>java com.martiansoftware.nailgun.NGServer
NGServer 0.9.2-SNAPSHOT started on all addresses, port 2113.
NGSession 1: 127.0.0.1: org.python.util.jython exited with status -1
java.lang.ClassCastException: org.python.util.JLineConsole$Stream cannot be cast
 to com.martiansoftware.nailgun.ThreadLocalInputStream
        at com.martiansoftware.nailgun.NGSession.run(NGSession.java:263)
Exception in thread "NGSession 2: 127.0.0.1: org.python.util.jython" java.lang.C
lassCastException: org.python.util.JLineConsole$Stream cannot be cast to com.mar
tiansoftware.nailgun.ThreadLocalInputStream
        at com.martiansoftware.nailgun.NGSession.run(NGSession.java:381)

I seem to be quite close ... does any Eclipse expert have any idea how I should proceed?

PS I've also tried running a trivial Jython program outside of Eclipse. So far it always trips up the Nailgun server as above. There is a bit of evidence out there of people doing it successfully, but they always seem to be on a Linux box, not Windows. Naturally enough I'm planning to take a look at the server source code where the errors are happening in due course.

回答1:

Result!

And it makes Jython start-ups super-fast. I recommend this to all Jythonistas.

As I realised eventually yesterday (PS comment), the first thing was to get Jython to run outside Eclipse. What I found I had to do was comment out 6 lines in the file nailgun-master\nailgun-server\src\main\java\com\martiansoftware\nailgun\NGSession.java:
ll 263-265
ll 381-383
And then rebuild the jar as per Nailgun setup instructions (i.e. mvn clean install).

After this, I made a handy .bat file to run the Nailgun server ("...\jars" is the place where I put nailgun-server-0.9.2-SNAPSHOT.jar and all the other jars I want to include):

set classpath=d:/apps/jython2.7.0/jython.jar;d:/apps/jython2.7.0/jars/*
java com.martiansoftware.nailgun.NGServer

After this, to run from a DOS window go:

ng org.python.util.jython "G:\My Documents\software projects\my_jython_project\__main__.py"

And then, in Eclipse, after following the steps above for setting up ng_client, select the .py file you want to run (in Project explorer window) and go

Run --> External tools --> ng_client

NB the sys.out & sys.err output is directed to the server window... presumably this is a consequence of the changes made to NGSession.java. If the Nailgun author, Marty Lamb, ever reads this, first Thanks for this great app, and second it'd be nice if you have any comments about the changes I made to NGSession.java.

later

Some strange things happen... hopefully just teething troubles.

In particular, I wanted to use this for unit testing which, like most development, involves constantly running things over and over. The trouble is I tend to find that the classes I am developing sometimes seem to "linger" in the "Nailgun memory". I've even seen classes run an older version of themselves when the NG server is stopped and a new one started... which is utterly baffling. It tends to suggest that older versions of .class files are somehow being "cached" in the Nailgun memory when I don't want them to be. And this happens even when using the "ng-cp" option to specify CLASSPATH additions in the client, not the server!

Unfortunately this is all quite "niche": using the Jython with the unittest module, running Nailgun. Sigh.



回答2:

Instead of making the code changes to Nailgun that Mike suggests doing to prevent the java.lang.ClassCastException, an alternative is to change the console used by Jython to the PlainConsole by supplying the following system property when you start Nailgun: -Dpython.console=org.python.core.PlainConsole This prevents the class cast exceptions and results in the console output being returned to the client rather than the server window too.