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!
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.
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).
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.
You may check Java VM parameters (like used GC, maximum memory etc). For data-intensive applications GC could slow things down a lot.
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.
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
Related
I have created an App using Java which treats excel files using Apache POI. The problem is that when I run the code from eclipse, it works fine, but when I made an executable jar for the app (Using eclipse export executable jar option), the jar is working fine but the results are different, even the size of the produced excel file is different.
I made many research, but I did not find a convenient solution.
Ah yes. I have had that same experience too a few years back.
When creating the runnable .jar in Eclipse, you can choose how .class files from libraries (such as Apache POI in this case) are handled:
Package required classes (.class files) into jar file
Package required libraries (.jar files) into jar file
Copy libraries into a sub-folder
Interestingly, with Apache POI, the three different ways of packing create HUGE differences:
In startup speed
In execution speed
In memory requirements (RAM)
In the resulting output files
I cannot recall which gave me the expected results.
So you have to try them out yourself. (Judging by how Eclipse starts Java projects, it should be #3, libs in subfolder, that gets you the same results). But: try the others anyhow; as I said, HUGE differences ahead.
TBH Apache POI is a 'good' example of how software should NOT be written.
It's awfully bloated and mega RAM hungry and has quite an interesting/odd behavior.
So I wrote my own lib for the newer .xls file format which is just a 100 times faster, smaller and more reliable. And does string caching and cell format operation optimization a lot better. So a 1000000 times better :-P
The upside is that the POI dev team knows the limitations and shortcomings of their project and offers multiple modes of processing files, to overcome said shortcomings. So, after all, kudos to them!
I'm very new to java. I'm developing a tool that checks if your PC meets some set of specifications. This included writing and executing a separate batch file and including an API (SIGAR) for the model of the CPU.
My problem is that when I tried exporting it to Runnable JAR in eclipse, and I ran the resulting JAR from command line, it gave me lots of 'DLL not included in build path' Exceptions. After including the folder that contains the API DLL in the build path, I got similar exceptions. The thing that fixed it was adding the folder containing the DLL to environment variables (PATH) in Advanced System Settings.
Questions:
The JAR now works fine on my computer, but what about the users who download the JAR? Will they also need to add the DLL to environment variables? If so is there a way the JAR can do that for them?
My JAR won't run with a double-click, but will run from command line. Is there any way around this that will carry over to users who download the JAR too?
If the user downloads the tool and can't run it because they don't have the right version of the JRE, will the tool notify them? If not, is there a way around the user having to update JRE or will wrapping as an EXE suffice?
Thanks in advance, much appreciated. Lots of questions.
Q1: The JAR now works fine on my computer, but what about the users
who download the JAR? Will they also need to add the DLL to
environment variables? If so is there a way the JAR can do that for
them?
You can put a DLL inside a JAR file:
How to make a JAR file that includes DLL files? (Hint: read both answers ... completely.)
However, when you distribute a JAR containing a DLL, you then have the problem that different platforms require different DLLs (or whatever). Even with Windows you have the problem of 32 bit versus 64 bit DLLs.
Q2: My JAR won't run with a double-click, but will run from command
line. Is there any way around this that will carry over to users who
download the JAR too?
You cannot address that problem in a JAR file. The "double-click to run" functionality is implemented by the OS. The best way to provide this kind of functionality is using (platform specific) wrapper scripts that are double-clickable.
Q3: If the user downloads the tool and can't run it because they don't
have the right version of the JRE, will the tool notify them? If not,
is there a way around the user having to update JRE or will wrapping
as an EXE suffice?
Unless you have a JRE installed, the JAR file is just a passive blob of data. (A ZIP file, actually).
If the user has a JRE that is too old, then either the JRE will be unable to load any classes in the JAR (because the classfile version number is wrong), or you will get errors because of missing (system) classes.
The only hope would to do something like providing a wrapper script to launch your application that checked the JRE version before attempting to launch the JAR.
As a general rule, if you want to do fancy stuff like this you need to distribute your program in an installer, not as a bare JAR file.
I am facing a strange problem. I have a project which is taking 3 to 4 minutes to execute while running through eclipse. But, the same project, taking 3 hours to complete the same task while extracting it as a runnable jar and running.
I have tried increasing heap size by running trough command line -Xmx=3600m and -Xms=1200m parameters.
Am I going in the right way. Is there any other ways to get down the execution time of runnable jar.
It might be worth comparing java client versions and ensuring that the machine running the jar is using the 64-bit architecture java client if available. Your eclipse runs using the JDK files (often 64-bit), but many systems only run 32-bit java clients for compatibility (specifically with browsers).
Is it possible that the jar file does not have all the libraries? Sometimes in my java programs, when an error is encountered, the process time increases significantly as the stack trace winds and unwinds. This is made worse if the error does not get handled properly.
Missing libraries could also cause this, as could running the jar in a different environment as the environment from whence you are running eclipse. Directory separators (if using files), or different ASCII characters getting brought in from APIs and such for different OS types.
Those are the causes I have noticed for unexpected java delays. Good luck!
Install Eclipse IDE and try Exporting as a Runnable Jar choosing following selection:
Option: 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.
I have this, perhaps, easy problem, but I don't know how to handle this.
I have my Java program and it works pretty well when I call it via terminal (Java command).
The program uses 4 text files from the hard disk which can't be added as resources to the project.
How to do this right so I could build jar file only with main class and files from hard disk (first one is a config file and it has paths to other files so the program knows where they are)?
I'm using IntelliJ IDEA 14.1.4 on Arch Linux.
I did it based on this blog, but it's not working without txt files in src folder.
Also "jar cvf" command builds jar file, but it's not working outside my computer (for example on windows or OSX).
Can anyone help me?
I prefer step by step instruction so I would understand what is going on in here.
I recommend to build your application with Maven and create a Maven Assembly which contains your JAR file as well as the config.txt file.
My program works perfectly fine when I run from eclipse but I package it to an executable jar and run it from command line, it runs much slower (almost five times slower). I am even setting the -vmargs while executing the jar
java -Xms40m -Xmx512m -jar jarFile.jar
I do not have any older version of java on my machine. I am not able to understand what I am missing over here. How can eclipse run the same program faster while using the same version of java and same vm arguments. Any help would be appreciated.
Thanks,
karthik
We had a similar issue but the problem was that while exporting Java code as a runnable jar file from eclipse, we were choosing an option "Package Required Libraries into generated jar". This was putting all of the referenced libraries as jar files within the runnable jar, which probably was being unpackaged during every run. Instead of that we used "Extract required libraries into generated jar" option while creating the runnable jar file and the speed of the execution of jar shot up - almost same as it was while running the code within eclipse.
You most likely print a lot to System.out/System.err (either directly or through logging). The Windows terminal emulator needs to render your output, and does it slower than Eclipse.
Try redirecting all output to a file or NUL and measure again.
Instead of checking "Package Required Libraries into generated jar",
use "Extract required libraries into generated jar" option while creating the runnable jar file. This saves a lot of time.
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.