11

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!

scaevity
  • 3,991
  • 13
  • 39
  • 54
  • 2
    Have you tried completely removing all the System.out.println() commands? Printing to console eats up a lot of speed. – Lai Xin Chu Jun 06 '12 at 17:57
  • 3
    I suggest using logger or log4j library to print log messages and you can use the time-stamp to see what part of your code is creating the bottleneck. This is not a concrete solution but it should help narrow down the problem. – user845279 Jun 06 '12 at 17:59
  • Agree with @Lai Xin Chu. But if it is difficult to remove all prints just run your program and redirect STDOUT to file. I do not believe that Eclipse' console can work slower than shell. – AlexR Jun 06 '12 at 18:00
  • @LaiXinChu There's no way each println() command can be slowing the program down by an entire minute... But I'll try it and report back in a while... – scaevity Jun 06 '12 at 18:00
  • @user845279 I'll try that and report back in a bit. – scaevity Jun 06 '12 at 18:01
  • Are perhaps eclipse and command line using different JVMs? – aepryus Jun 06 '12 at 18:04
  • @aepryus Nope, I'm specifying the JVM for both specifically, and they're the same. – scaevity Jun 06 '12 at 18:06
  • Its actually quite possible that 10 println() commands can slow your program down if its continuously looping. Like the rest have suggested, its a good idea to print them into a file instead. – Lai Xin Chu Jun 06 '12 at 18:07
  • you don't have to write additional code. Just do this: "java -jar program.jar 1>out.log" The stdout will be redirected to out.log – Lai Xin Chu Jun 06 '12 at 18:09
  • Have you tried to profile your app with something like YourKit? – keuleJ Jun 06 '12 at 18:12
  • @LaiXinChu Ok, so I tried removing the System.out.println() commands and there was very little difference in the speed (I may have been unclear, but only 10 lines are actually printed, the println() commands are not repeated in a loop or anything). – scaevity Jun 06 '12 at 19:09
  • I see. Ok then another suggestion I have for you is that, since you constantly have to access the same database, you can keep a database connection open for an extended period, instead of closing the connection and reopening it repeatedly. That is also another cause of a slow program. You should use a profiling tool to see which part of your program is eating up the speed. – Lai Xin Chu Jun 07 '12 at 02:09
  • @LaiXinChu But is there any reason that that would be any slower for the .jar file than in eclipse, though? – scaevity Jun 07 '12 at 15:14
  • well i can think of a couple reasons. its also possible that eclipse gave your JRE more heap memory to run your program. So you could try running your jar file with a higher heap memory? There are many possible reasons – Lai Xin Chu Jun 07 '12 at 17:24
  • If you are familiar with algorithm analysis, or know someone who is familiar with it, you can attempt to calculate the Big-O notation of your algorithm as well. Not sure how much data you are reading, but 27 minutes seem rather taxing. – Lai Xin Chu Jun 07 '12 at 17:31

6 Answers6

13

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.

Alekhya Vemavarapu
  • 1,145
  • 1
  • 10
  • 26
  • 1
    same here, running in eclipse was fine (nearly instant), in the jar the process was 4 minutes behind. your suggestion solved my problem – benez May 12 '16 at 15:28
4

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).

npe
  • 15,395
  • 1
  • 56
  • 55
4

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.

BigMan73
  • 1,344
  • 15
  • 14
  • Thank you for this! Apparently that also has an enormous impact on the FXMLLoader class of JavaFX, causing it to work about 10 times slower with a "packed jar" and that although the files were outside unpacked already... – DragonGamer Jul 28 '17 at 04:46
  • Same result here, also I didn't need the `-cp`, as the manifest file already specifies the location of the sub-JARs – golimar Nov 28 '17 at 12:02
1

You may check Java VM parameters (like used GC, maximum memory etc). For data-intensive applications GC could slow things down a lot.

n3o59hf
  • 469
  • 6
  • 10
0

Yes I have the same problem. I have Experimented and got a solution!

just choose "Package required libraries into jar" while making the jar files. this solution worked fine with me and hope this will also for for you to.

  • 1
    So you're saying that this _sped up_ your program? I ask because this is the exact opposite guidance offered by [Alekhya Vemavarapu in the top-rated answer](https://stackoverflow.com/a/33293652/3025856). – Jeremy Caney Sep 01 '20 at 19:23
0

I faced the same issue. The eclipse took 5 seconds to run the application while the jar took 3 minutes. This is due to the way I exported the runnable jar file.

These are mainly two ways to export as a Runnable jar in eclipse.

1). Package required libraries into jar

  • This will add actual jar files of the libraries into your jar. This is the cleanest since it separates application class files with library JARs.
  • The downside is this makes runnable jar perform very slow.

2). Extract required libraries into generated jar

  • This way will extract only the actual class files from the libraries that your application uses and includes in the runnable jar file. As a result your Jar file will include your application class files as well as class files of all the libraries used by your application.

  • This method makes runnable jar perform just like it is run in your eclipse IDE.

  • Using this way I was able to run jar application without any lag and completed just as I run in eclipse IDE taking 5 seconds.

Having said that, best approach would be to use maven build tool. By using Maven it is very easy to maintain and handle third party libraries. You may have a look.

https://www.baeldung.com/executable-jar-with-maven

Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51