Java 代码通过 IDE 运行但从终端失败
我有一个 Java 代码,我可以使用自定义配置在 Intellij 上运行它。配置具有以下属性:-
- module:java 8 (Oracle OpenJDK 1.8.0_321)
- classpath:-cp XYZ.main()
- main:com.ABC.XYZ.ManageTraffic
- CLI 争论:server XYZ.yml
但是当我尝试运行jar 是使用 gradle 从终端构建的,它给了我错误,无法找到或加载主类 com.ABC.XYZ.ManageTraffic
到目前为止我有尝试了以下方法,查看 Stackoverflow 上的其他解决方案,仍然遇到相同的错误
- java -jar ques.jar
- java -jar ques.jar com.ABC.XYZ.ManageTraffic
- java -cp /build/libs/ques.jar com.ABC. XYZ.ManageTraffic
只是为了交叉检查,我解压了创建的 jar 并发现 com.ABC.XYZ.ManageTraffic 类文件在那里可用,但仍然出现错误。可能是什么问题?
I have a Java Code where I am able to run it on Intellij using custom configuration. The configuration has following attributes :-
- module : java 8 (Oracle OpenJDK 1.8.0_321)
- classpath : -cp XYZ.main()
- main : com.ABC.XYZ.ManageTraffic
- CLI arguements : server XYZ.yml
But when I try to run the jar that was build using gradle from terminal , it gives me Error , could not find or load main class com.ABC.XYZ.ManageTraffic
So far I have tried the following things looking at other solutions at Stackoverflow , still getting the same error
- java -jar ques.jar
- java -jar ques.jar com.ABC.XYZ.ManageTraffic
- java -cp /build/libs/ques.jar com.ABC.XYZ.ManageTraffic
Just to cross check , I unzipped the creataed jar and found that com.ABC.XYZ.ManageTraffic class file is available there , still getting error. What could be the issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 IDE 运行它,并在运行时尝试从进程列表中获取 IDE 使用的命令。
不确定你正在使用什么操作系统,但类似的东西应该在 linux/mac 上工作:
在你拥有命令之后,你可以尝试理解为什么它不适合你,或者只是使用该命令
Run it from the IDE, and while it is running, try to get the command used by the IDE from the process list.
not sure what OS you are using but something like this should work on linux/mac:
After you have the command you can try to understand why its not working for you, or just use that command
只是想添加我如何设法运行它。我创建了同一应用程序的新阴影 jar 文件。刷新了它的依赖关系,现在它可以工作了。我还没有弄清楚创建一个阴影罐子而不是普通罐子有什么帮助。现在我能弄清楚的唯一原因是可能与某些依赖项存在版本冲突,但我想知道它如何抛出找不到主类错误。
无论如何,然后我从终端使用以下命令运行该文件:
java -jar ./build/libs/ques-shaded.jar 服务器 XYZ.yml
Just want to add how I managed to run it. I created a new shaded jar file of the same application. refreshed its dependencies and now it works. I am yet to find out how creating a shaded jar instead of normal jar helped. Right now the only reason I could figure out is there may be version clashes with some dependencies but I wonder how it could throw could not found main class error.
Anyways , then I ran the file with the following command from terminal:
java -jar ./build/libs/ques-shaded.jar server XYZ.yml