简体   繁体   中英

Command line tool in Java gets run many times in a row, JRE keeps opening/closing, slow

I wrote a command line tool in Java and it gets run many times in a row from a BAT script. Right now it's a runnable JAR (which in hindsight isn't probably the right choice here...). The JRE seems to get loaded and unloaded every time the program is run, which seems inefficient. Overall, it seems a lot slower than it should be.

Do any Java gurus out there know a more proper way to handle this situation?

You could try a number of things. A simple one is starting the JVM with -Xquickstart .

EDIT : Looks like this option is for the IBM JDK only (which incidentally also has support for AOT-compilation/shared class caches).

Another option is, making it into a single longer running process that can accept multiple jobs interactively or in a batch.

Someone created a project to make a framework for this purpose called nailgun , not sure what state it is in. Google gave me these results when I searched: http://sourceforge.net/projects/nailgun/ and http://martiansoftware.com/nailgun/ .

There is also a GNU's gcj which will compile Java down to native code http://gcc.gnu.org/java/ .

As there is no easy fix, you might also consider more radical approaches:

  1. Actually porting what's in your bat in the java itself (easy if it's loop on files with some kind of filter).

  2. Compiling the jar into an exe (eg with GCJ) if maximum portability is not an issue.

This is actually one of the biggest problems with Java, and there aren't any easy solutions. Basically, if you want a lightweight program that might get run many times in rapid succession, then you shouldn't be doing it in Java.

I don't have any experience with compiling Java to native code, but I do know that almost no one does it; there might be a good reason for that.

If you post the BAT file, or at least describe what it does, maybe someone can offer a better suggestion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM