简体   繁体   English

导出时Java App出现意外行为

[英]Java App unexpected behavior when exported

I have a very strange problem, that I can't figure out, the thing is that my aplication runs perfectly on the IDE (Eclipse), but not when exported, when I run the jar (double click) the aplication start but some functionality is missing (loading from a template file, but this does not happend when loading from a normal file), when I try to run it from console (java - jar my.jar) in order to see any error message it turns out that my aplication works perfectly fine! 我有一个非常奇怪的问题,我无法弄清楚,问题是我的应用程序可以在IDE(Eclipse)上完美运行,但是不能在导出时运行,当我运行jar(双击)时,应用程序开始但有一些功能丢失(从模板文件加载,但是从普通文件加载时不会发生),当我尝试从控制台(java-jar my.jar)运行它以查看任何错误消息时,发现我乘法效果很好! :S ... :S ...

Some more info: 更多信息:

My app is running over windows 7 我的应用程序在Windows 7上运行

I start the task manager, and I noticed that when I start my aplication using double click its under the name java.exe *32, and when I do it from command line its under the name java.exe (without "*32"), as far as I know I programmed nothing related to a 32 or 64 bits functionallity. 我启动了任务管理器,并注意到当我使用名称为java.exe * 32的应用程序双击时,以及当我从命令行中以java.exe的名称进行操作(不带“ * 32”)时,我都注意到了。据我所知,我没有编写任何与32位或64位功能相关的内容。

"Solved" “解决了”

Well I was not able to solve it the way I wanted, as far as I was able to find, i found that there were a problem between the 2 java versions I was running x32 & x64, I deleted the 32 bit version and it start working as a charm, but I'm still not sure about what happend, I give my thanks to @Sajal Dutta one of its comments help me to understand part of the problem, thanks to all of you anyway, I'll keep searching until I find the problem... 好吧,我无法以自己想要的方式解决它,据我发现,我发现在运行x32和x64的2个Java版本之间存在问题,我删除了32位版本并开始作为魅力,但我仍然不确定发生了什么,我感谢@Sajal Dutta,其中之一的评论可以帮助我理解问题的一部分,无论如何,感谢大家,我将继续搜索直到我发现问题了...

When you create a jar from Eclipse, your assets don't get copied over to jar or location is not preserved. 从Eclipse创建jar时,不会将您的资产复制到jar上,也不会保留位置。 Open the jar and check if you have your templates in the right location or you have it at all. 打开罐子,检查模板是否在正确的位置,或者完全没有模板。

To have the exported jar include your assets/resources- 要使导出的jar包含您的资产/资源,

  1. Right click on your project in Eclipse. 在Eclipse中右键单击您的项目。 Then New -> Source Folder. 然后新建->源文件夹。
  2. Name the source folder anything. 将源文件夹命名为任何名称。 eg template_src. 例如template_src。
  3. Copy or drag the entire directory of your template to template_src. 将模板的整个目录复制或拖动到template_src。 Then make the jar. 然后做罐子。

Since it works via the command line but not when double-clicking the jar, it is likely that the working directory is different (and that you're loading the template with a relative path). 由于它是通过命令行运行的,而不是双击jar时无法运行的,因此工作目录可能不同(并且您正在使用相对路径加载模板)。 When you run an executable jar by double-clicking, on some operating systems, the working directory is the home directory whereas when you run from the command line, it's the directory you're currently in. 在某些操作系统上,通过双击运行可执行jar时,工作目录为主目录,而从命令行运行时,则为当前目录。

The "files" in the jar are not handled by File, but are resources; jar中的“文件”不是由File处理,而是资源。

URL url = getClass().getResource("...");
InputStream in = getClass().getResourceAsStream("...");

Then, the file paths inside a jar, or on a non-Windows platform are case-sensitive . 然后,jar内或非Windows平台上的文件路径区分大小写

"Template_A.xml"

is not 不是

"template_a.xml"

Also you might inspect the jar with 7zip or WinZip. 您也可以使用7zip或WinZip检查罐子。

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

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