Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my .jar file running slower than the program in eclipse?

I have a java program that reads a lot of input data from a database, manipulates it, then writes data back out to another database (using ODBC drivers, excel and access databases, on a new windows 7 machine). The program takes about 17 minutes to run from eclipse, but when I created an executable .jar file it takes an extra 10 minutes to run (27 total).

The two reasons I've found so far for slow jar files (by searching SO and google) is that they're compressed and that it takes a lot longer to write to the command prompt (or error log) than the console in eclipse. I tried creating an uncompressed jar file and it only sped up by about 10 seconds (which could have been completely random, as the run times vary by about 30 seconds anyways). I only have about 10 System.out.println() commands in the program, so that shouldn't be slowing it down much.

Any ideas as to what is causing it to run so much slower, and if there is any way I can speed it up again? Let me know if there are any other detail that may be relevant that I should include. Thanks!

like image 378
scaevity Avatar asked Jun 06 '12 17:06

scaevity


3 Answers

In my case, my application took 3 secs to run on eclipse while it took 2 mins when I run it from jar.
My mistake was to choose "Package required libraries into jar" while exporting my project into runnable jar.

I tried various ways to bring down the time but nothing helped, except..

If you have other maven dependencies or jar files in your project, you should use "**Extract required libraries into generated jar**" while exporting your project into a jar.

This solved my problem in seconds & now both my eclipse & jar file are taking same time to run the application, 2 secs.

Hope this helps the new strugglers.

Regards.

like image 94
Alekhya Vemavarapu Avatar answered Oct 06 '22 13:10

Alekhya Vemavarapu


Use JAMon. It's a monitoring library, that will help you measure execution times of your code.

After you add some monitoring code to your methods, run it in Eclipse and as a JAR file, and compare the results. This should allow you to narrow the search.

Also: Check, whether you are running your JAR file, with the same java version, that the Eclipse uses (for example Java 1.4.x may be much slower than 1.6.x).

like image 30
npe Avatar answered Oct 06 '22 11:10

npe


I had a similar problem. The shell was running orders of magnitude slower and it had nothing to do with console output. I tried setting JVM memory values but it didn't make any difference

The solution was to package the ANT file with all the JARs into an external folder, using the "Copy required libraries into a sub-folder next to the generated JAR" option in the "Runnable JAR File Export" wizard. Then run the main JAR with a -cp [YOURSUBFOLDER] command line option.

like image 26
BigMan73 Avatar answered Oct 06 '22 11:10

BigMan73