Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Expected algorithm:

  1. You start JVM without actual application (only telling it to load some jars), it loads and listens a socket and waits in a background.
  2. When you start the application (preloaded_java -cp /usr/share/java/....jar:. qqq.jar) it connects to the existing loaded JVM, loads additional jars (if any) and executes main class.
  3. preloaded_java just routes input and output and handles interrupts etc.

Update Implemented a proof of concept:

$ clojure prejvm.clj&
[1] 2883
$ nc 7711 <<< '{"mainclass" "test.Hello"}'
$ nc 7712
java.lang.ClassNotFoundException: test.Hello
    at clojure.main.main(

$ nc 7711 <<< '{"classpaths" ["file:///home/vi/code/prejvm/"], "mainclass" "test.Hello"}'    
$ nc 7712
Hello, world; number of args is 0
qwe q e32e qda
qwe q e32e qda

$ nc 7711 <<< '{"classpaths" ["file:///home/vi/code/prejvm/"], "mainclass" "test.Hello", "argv" ["qqq" "www" "eee"]}'
$ nc 7712    
Hello, world; number of args is 3
sdfasdfasf df sad
sdfasdfasf df sad

Update 2: Found answer myself: Nailgun server (from VimClojure).

share|improve this question
up vote 2 down vote accepted

Use Nailgun Server:

Start it:

java -classpath /usr/share/java/clojure.jar:/usr/share/java/clojure-contrib.jar com.martiansoftware.nailgun.NGServer

Use it:

$ ng clojure.main 
Clojure 1.2.0

$ ng test.Hello sdf sdf sdf
Hello, world; number of args is 3

It can be obtained with VimClojure:

share|improve this answer
A quick perusal does not reveal any class loader tricks, so a class will only be initialized once per server run, and not "per use". This may or may not have an effect on the correctness of whatever you are running under NailGun. – Ken Sep 18 '10 at 0:48
If some code fails on NailGun because of this then it is probably not well-written. In a good program "initialized once per server run" should just mean that it would be even more fast. – Vi. Sep 18 '10 at 17:26

No, what you describe is not possible, at least not with the Sun JVM, simply because it's not implemented (though it's a nice idea).

However, if you just run the app once, the OS will cache the data in RAM that was loaded from disk, so subsequent starts should be a lot faster. That might already help.

If you need a higher speedup: Can you maybe change the app, such that it just keeps running and accepts new input?

share|improve this answer
I want more-or-less universal approach. Looks like the most complex part for it will be "pseudo-java runner" that read arguments and connects to the java daemon. – Vi. Sep 14 '10 at 23:11
"... is not possible, at least not with the Sun JVM" Why it can't be third-party application? All necessary features are already present in JVM: our app can load class and execute main (redirecting System.{in,out,err}) somehow. – Vi. Sep 14 '10 at 23:14
It's odd that Sun (/Oracle) doesn't do this, since it's been a feature of several "server" JVMs since 1998 or so. – Daniel R Hicks Nov 24 '13 at 2:01

Sure it can be done. What you describe is actually pretty close to how a Java EE application server works.

share|improve this answer
So it should be like Java application server for console programs. – Vi. Sep 15 '10 at 10:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .