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.
Related
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 using Excelsior JET to convert my JAR into an EXE.
I want my application to be Standalone. Literally one single file without needing installation. JET creates an EXE and also a runtime folder.
The solution, according to them, is to use 7z to create a self-extracting file (into a temporary) that will run the application.
The problem is that the self-extraction takes a long time, so program startup is ridiculous.
So my question is: is there another alternative to pack my EXE and runtime into a single standalone file?
I am using launch4j to wrap up my jars as .exe - works fine for me so far. I read about how people managed to wrap the JRE into the file using launch4j in this post:
How to bundle a JRE with Launch4j?
hope that helps - never tried myself...
DISCLAIMER: I work for Excelsior.
There were some changes made in Java 8 aimed at enabling the creation of standalone executables, such as JEP-178. It was unfeasible for us to implement this feature ourselves for earlier versions of Java. That said, we have only moved to Java 8 in Sep'15, and standalone executables are not in our near- to mid-term plans due to (relatively) low customer demand.
I finished a small program. What is the standard file type for the final application written with Java, so it can be run on any computer, easily and without any computer knowledge?
I've been told it's JAR, but Eclipse for example is an .exe file.
What's the standard file type for big, normal applications in Java?
Are most applications distributed in JAR, or rather in .exe or something else?
Serious desktop applications are packaged with platform-specific launchers, which are not written in Java. The launcher must first find out how to run the JVM installed on the system, and then pass it either the path to the executable JAR to run, or the complete classpath along with the name of the main class.
In other words, "it's complicated".
Most desktop applications are distributed using .jar files. A .exe is windows-specific, and non-portable across different operating systems. It's easy to find installers (or "launchers") that will simplify the distribution of a Java program in other platforms, but anyway you'll find that .jar files are the usual packaging mechanism.
If you have a small, simple Java program the easiest approach to distribute it would be to pack it in a .jar, making sure to make it executable. And remember, the computer where your code is expected to run must have installed some version of Java, be it JRE or JDK.
Desktop java applications are usually distributed as jar files.
JRE can launch a runnable jar file using -jar param.
You have one of several options:
1 - Create an executable jar file. By providing information in a manifest within the jar file users can simply execute the jar file by however system-dependent means exist for their OS.
2 - Write a batch file or shell script to invoke the JRE against your jar file (and specify command line parameters for, eg: the main class, the classpath, JVM options, etc.)
3 - Use a tool like jexepack or jsmooth to wrap your Java code within a native executable. I've only ever used these to create Windows binaries - there may be other options for other platforms but shell scripts are typically easier to work with here.
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
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.