Java reflection efficiency drops when packaged as a jar

I have a real head here. I tried everything, searched everywhere. This comes from an application that I have inherited from test JARs.

(It consists of a GUI and a command line application that does the actual verification. The GUI launches the command line application by launching a new JVM on itself [java -cp "yourself.jar" com.different. MainClass]. This is a bad design, I know but may be relevant.)

In any case, this program contains some reflection calls nested inside two for-loops. The problem is that when the application is JARed up, the first reflection call takes exactly one second at each iteration. But when it starts from classes, it takes a few milliseconds.

In practice, this means the following command:

java -jar myjar.jar 

takes hours.

This command:

 java -cp "...[bunch of jars];myjar.jar" com.myclasses.main 

takes minutes.

A verified JAR is always a bank. The difference is only in the test application.

Any ideas or opportunities to pursue are greatly appreciated. Thanks!

+7
source share
3 answers

You can run your program under a profiler, for example, Eclipse TPTP or YourKit and more accurately determine where your time is spent. This is likely to indicate an error in your code, or somewhat less likely to indicate an error in the library. Then, if you cannot figure it out, post the appropriate code here, and we can help.

+1
source

I don’t know if this is related to your problem or not, but the arguments “-cp” in this command will be ignored:

 $ java -cp "...[bunch of jars]" -jar myjar.jar 

The -cp and -jar command options cannot be used together. The executable JAR file gets its class path from the JAR manifest.

I don’t see how this explains the symptoms that you see ... but it means that what you are trying to do with the executable JAR will not work, and this may make your question moot.


FOLLOWUP

Given that this was just a mistake in the question ... not a real problem ...

My suggestion is that you run your tests using the 2nd form of the java command; i.e. with the -cp and classpath option. It’s hard to understand how this will affect the effectiveness of your test mode.

I suspect that identifying the root cause of a performance problem will entail a detailed review of what the tester application does.

0
source

As others have already mentioned, you really need to profile the code to see where it spends its time.

If I were to guess, I would suspect that the work of reflections leads to a repeated search for the class loader and loading the class more slowly when extracting the jar from the file.

But then again, this is just a hunch. You should comment on this.

0
source

All Articles