简体   繁体   English

为什么我的.jar文件运行速度比eclipse中的程序慢?

[英]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).我有一个 java 程序,它从数据库读取大量输入数据,对其进行操作,然后将数据写回另一个数据库(使用 ODBC 驱动程序、excel 和访问数据库,在新的 windows 7 机器上)。 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).该程序从 eclipse 开始运行大约需要 17 分钟,但是当我创建一个 executable.jar 文件时,它需要额外运行 10 分钟(总共 27 分钟)。

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).到目前为止,我发现 jar 文件(通过搜索 SO 和谷歌)速度慢的两个原因是它们被压缩并且写入命令提示符(或错误日志)比 eclipse 中的控制台花费更长的时间。我尝试创建一个未压缩的 jar 文件,它只加速了大约 10 秒(这可能是完全随机的,因为运行时间无论如何都会有大约 30 秒的变化)。 I only have about 10 System.out.println() commands in the program, so that shouldn't be slowing it down much.我在程序中只有大约 10 个System.out.println()命令,所以这不会减慢它的速度。

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.就我而言,我的应用程序在 eclipse 上运行需要 3 秒,而从 jar 运行它需要 2 分钟。
My mistake was to choose "Package required libraries into jar" while exporting my project into runnable jar.我的错误是在将我的项目导出到可运行的 jar 时选择了"Package required libraries into 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.如果您的项目中有其他 maven 依赖项或 jar 文件,则在将您的项目导出"**Extract required libraries into generated jar**"时应使用"**Extract required libraries into generated jar**"

This solved my problem in seconds & now both my eclipse & jar file are taking same time to run the application, 2 secs.这在几秒钟内解决了我的问题,现在我的 eclipse 和 jar 文件都花费了相同的时间来运行应用程序,2 秒。

Hope this helps the new strugglers.希望这对新的奋斗者有所帮助。

Regards.问候。

Use JAMon .使用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.在向方法中添加一些监视代码后,在 Eclipse 中将其作为 JAR 文件运行,然后比较结果。 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).另外:检查您是否正在使用 Eclipse 使用的相同 Java 版本运行 JAR 文件(例如 Java 1.4.x 可能比 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. shell 运行速度慢了几个数量级,它与控制台输出无关。 I tried setting JVM memory values but it didn't make any difference我尝试设置 JVM 内存值,但没有任何区别

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.解决方案是使用“Runnable JAR File Export”向导中的“Copy required libraries into a sub-folder next to the generated JAR”选项将带有所有 JAR 的 ANT 文件打包到一个外部文件夹中。 Then run the main JAR with a -cp [YOURSUBFOLDER] command line option.然后使用 -cp [YOURSUBFOLDER] 命令行选项运行主 JAR。

You may check Java VM parameters (like used GC, maximum memory etc).您可以检查 Java VM 参数(如已使用的 GC、最大内存等)。 For data-intensive applications GC could slow things down a lot.对于数据密集型应用程序,GC 可能会大大减慢速度。

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.只需在制作 jar 文件时选择“将所需的库打包到 jar 中”即可 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. eclipse运行应用程序需要 5 秒,而jar需要 3 分钟。 This is due to the way I exported the runnable jar file.这是由于我导出可运行的jar文件的方式所致。

These are mainly two ways to export as a Runnable jar in eclipse .这主要是在eclipse中导出为 Runnable jar的两种方式。

1). 1). Package required libraries into jar Package需要的库改成jar

  • This will add actual jar files of the libraries into your jar .这会将库的实际jar文件添加到您的jar中。 This is the cleanest since it separates application class files with library JARs.这是最干净的,因为它将应用程序 class 文件与库 JARs 分开。
  • The downside is this makes runnable jar perform very slow.缺点是这会使可运行的 jar 执行非常慢。

2). 2). Extract required libraries into generated jar将所需的库提取到生成的 jar

  • This way will extract only the actual class files from the libraries that your application uses and includes in the runnable jar file.这种方式只会从您的应用程序使用的库中提取实际的 class 文件,并将其包含在可运行的jar文件中。 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.因此,您的 Jar 文件将包含您的应用程序 class 文件以及您的应用程序使用的所有库的 class 文件。

  • This method makes runnable jar perform just like it is run in your eclipse IDE.此方法使可运行的 jar 的性能就像在您的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.使用这种方式,我能够毫无延迟地运行 jar 应用程序,并且就像我在eclipse IDE 中运行一样,耗时 5 秒。

Having said that, best approach would be to use maven build tool.话虽如此,最好的方法是使用maven构建工具。 By using Maven it is very easy to maintain and handle third party libraries.通过使用Maven可以很容易地维护和处理第三方库。 You may have a look.你可以看看。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM