从命令行运行时将jar添加到类路径 [英] Adding jar to classpath when running from command line

查看:158
本文介绍了从命令行运行时将jar添加到类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的我知道这个问题之前已被问过很多次,但是我用Google搜索并查看了一些例子并查看了过去一个月的SO问题,我真的无法让这个问题起作用。我认为问题是我希望能够从Eclipse和命令行运行该程序。我也在使用OSX,我认为我正在阅读的很多例子都是针对Windows / Linux的。

Ok I know this question has been asked many many many times before, but I've googled it and looked at examples and looked at questions on SO for the past month, and I seriously cannot get this to work. I think the problem is that I want to be able to run the program from both Eclipse and the command line. I'm also using OSX and I think a lot of the examples I'm reading are for Windows/Linux.

如果我在Eclipse中编译了一个简单的程序,那么我想要从命令行运行我这样做:

If I have a simple program compiled in Eclipse that I want to run from the command line I do this:

java -cp bin MyProgram

我有另一个程序,我在Eclipse中编译和运行,这引用了MySQL JDBC连接器(mysql-connector-java-5.1.19-bin。 jar)存储在同一目录中。这可以在Eclipse中正常工作,但我无法从命令行运行它。

I have another program I compile and run in Eclipse, and this references the MySQL JDBC connector (mysql-connector-java-5.1.19-bin.jar) which is stored in the same directory. This works fine from Eclipse, but I can't run it from the command line.

我已经尝试了所有组合......

I've tried all combinations of things...

java -classpath "bin;mysql-connector-java-5.1.19-bin.jar" MyProgram
java -cp bin\;mysql-connector-java-5.1.19-bin.jar MyProgram

并获得各种类发现错误...

and get all sorts of class not found errors...

Exception in thread "main" java.lang.NoClassDefFoundError: MyProgram
Caused by: java.lang.ClassNotFoundException: MyProgram
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)


推荐答案

您的问题是分分离器所使用。分隔符; 适用于Windows。在Unix系统上,你应该使用

Your problem is min separator you are using. Separator ; is for windows. On Unix systems you should use : instead:

java -classpathbin:mysql-connector-java-5.1 .19-bin.jarMyProgram

java -classpath "bin:mysql-connector-java-5.1.19-bin.jar" MyProgram

这篇关于从命令行运行时将jar添加到类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆