[英]JAR File sometimes runs but doesn't work, or doesn't run at all
I created a GUI program that involves making many API calls in Eclipse. 我创建了一个GUI程序,其中涉及在Eclipse中进行许多API调用。 Works completely fine when running from Eclipse, BUT...
在Eclipse,BUT上运行时工作正常
I exported my program as a JAR file and when I try to run it, from the terminal or from double-clicking, it throws a NoClassDefFoundError. 我将程序导出为JAR文件,并尝试从终端或双击运行该程序时,将引发NoClassDefFoundError。 I made sure all the packages were selected and the right Main-Class was chosen, still no luck.
我确保选择了所有软件包,并选择了正确的Main-Class,仍然没有运气。
I tried exporting it as a Runnable JAR file and when I double-click it, it runs and opens the GUI but doesn't work as intended. 我尝试将其导出为可运行的JAR文件,当我双击它时,它会运行并打开GUI,但无法正常工作。 However, when I run it from the terminal, it works as intended.
但是,当我从终端运行它时,它可以按预期工作。 I have no clue why.
我不知道为什么。
The error from the JAR file I get in the terminal is: 我在终端中收到的JAR文件中的错误是:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Don't really know the solution to this, as some sources online say to change things on my machine that involves admin access, which I dont have because I work on a company laptop. 真的不知道解决方案,因为一些在线消息人士说要更改我的机器上涉及管理员访问权限的内容,这是我在公司笔记本电脑上工作所没有的。
For the Runnable JAR file, my GUI works as intended up until the first HttpRequest. 对于Runnable JAR文件,我的GUI可以按预期工作,直到第一个HttpRequest为止。 I have to request an access token for oAuth, but my request isnt being made or something, because I'm getting a null return for which I check for.
我必须为oAuth请求访问令牌,但是我的请求没有被执行,因为我得到了要检查的空返回值。
Here is the code where that gets printed: 这是打印的代码:
try {
HttpPost request = new HttpPost(url);
request.addHeader("Content-Type", "application/json");
StringEntity params;
params = new StringEntity("{\"grant_type\": \"authorization_code\","
+ "\"client_id\": \"\","
+ "\"client_secret\": \"\","
+ "\"code\": \"" + code + "\"," + "\"redirect_uri\": \"\"}");
request.setEntity(params);
String content = execute(request, client);
if (content != null) {
textArea.append("Response received\n\n");
} else {
textArea.append("An error occurred. No response received, please restart the process.");
return;
}
Just to be clear, the program works perfectly fine when running from Eclipse. 需要明确的是,从Eclipse运行时,该程序运行良好。
Not really sure how to fix this, if anyone has any ideas I would appreciate your help :) 不太确定如何解决此问题,如果有人有任何想法,我将感谢您的帮助:)
That error means org.apache http-client library is not in the jar. 该错误意味着org.apache http-client库不在jar中。 Probably you have it linked in your Eclipse project, but it is not exported into the jar.
可能您已将其链接到Eclipse项目中,但未将其导出到jar中。
You have to create runable jar ("fat-jar") This is Eclipse guide: 您必须创建可运行的jar(“ fat-jar”),这是Eclipse指南:
Select library handling strategy: Package required libraries into generated JAR 选择库处理策略:将所需的库打包到生成的JAR中
If that doesn't help, please provide screenshots, how you export this jar from Eclipse, and how org.apache http-client is linked. 如果那没有帮助,请提供屏幕截图,如何从Eclipse导出此jar以及如何链接org.apache http-client。
I figured out a quick fix. 我想出了一个快速解决方案。 After exporting the Runnable JAR, create a shortcut.
导出Runnable JAR之后,创建一个快捷方式。 Right-click the shortcut and choose Properties.
右键单击快捷方式,然后选择“属性”。 Under the Shortcuts tab, look for target and change it to "C:\\Program Files\\Java\\jdk1.8.0_192\\bin\\javaw.exe" -jar "C:\\Users\\{user}\\Documents\\program.jar"
在“快捷方式”选项卡下,查找目标并将其更改为“ C:\\ Program Files \\ Java \\ jdk1.8.0_192 \\ bin \\ javaw.exe” -jar“ C:\\ Users \\ {user} \\ Documents \\ program.jar”
So the location of your javaw.exe file, followed by -jar and then the location of the jar file. 因此,您的javaw.exe文件的位置,然后是-jar,然后是jar文件的位置。
Then hit apply and close the window. 然后点击应用并关闭窗口。 My program worked after that.
之后,我的程序开始工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.