简体   繁体   English

无法从Shell ClassNotFoundException运行Java程序

[英]Unable to run a Java program from shell ClassNotFoundException

I am trying to run a Java program from the bash shell. 我正在尝试从bash shell运行Java程序。 The program uses some 3rd party libs (mysql connector , amazon aws) 该程序使用了一些第三方库(mysql连接器,亚马逊aws)

This is just a standard java program , not running on a J2EE server or anything complicated. 这只是一个标准的Java程序,不能在J2EE服务器或任何复杂的程序上运行。

It always complains that it cannot find one of the classes in the third party libs (ClassNotFoundException) (If I remove references to this then it will complain about a different one). 它总是抱怨它找不到第三方库(ClassNotFoundException)中的一个类(如果我删除对此的引用,那么它将抱怨另一个)。

This is confusing since I have checked that the location of the jar files containing the libs is in the classpath. 因为我已经检查了包含libs的jar文件的位置在类路径中,所以这令人困惑。 I have also tried putting the libs in the same folder as the .class File I am trying to run to no avail. 我也尝试过将这些库文件与试图运行的.class文件放在同一文件夹中,但无济于事。

I compiled the program using the javac command on the same computer and it had no problems finding the libraries , so I am confused as to why it cannot find them at runtime.. 我在同一台计算机上使用javac命令编译了该程序,并且找不到库没有问题,因此我对为什么在运行时找不到它们感到困惑。

I also tried this from cmd under windows but got the same problem. 我也在Windows下的cmd中尝试了此操作,但遇到了同样的问题。

The only rational explaination I can find is that it is due to security restrictions in the JVM , in which case it would be good to know how to disable them for this program? 我能找到的唯一合理的解释是,这是由于JVM中的安全限制所致,在这种情况下,最好知道如何为此程序禁用它们?

Thanks 谢谢

Kind of what Bozho said but for jar files you have to call out the jar file by path AND name, just not the path to them. Bozho所说的有点,但对于jar文件,您必须通过路径AND名称(而不是它们的路径)来调用jar文件。 From what I gathered from your post you are only adding the path. 根据我从您的帖子中收集的信息,您仅添加了路径。

It would be better if you had put the commandline that you are using in the question. 如果您将正在使用的命令行放在问题中,那会更好。 It is basically something like this java -classpath c:/libs/myjar.jar;c:/libs/myjar2.jar;c:/myclasses MyClass where MyClass is your "main" java class the -classpath option is the list of areas where you want the runtime to look for dependencies. 基本上是这样的java -classpath c:/libs/myjar.jar;c:/libs/myjar2.jar;c:/myclasses MyClass其中MyClass是您的“主要” java类,-classpath选项是区域列表您希望运行时在其中查找依赖项的位置。 The .jar are jar files and the folder myclasses is a folder where you may have a .class files or other config information that the program may need. .jar是jar文件,而myclasses文件夹是一个文件夹,您可能在其中拥有.class文件或程序可能需要的其他配置信息。

I figured this out for Linux, it's java -classpath "/path/to/jar/myjar.jar:." 我在Linux上发现了这个问题,它是java -classpath“ /path/to/jar/myjar.jar:”。 MyClass 我的课

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

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