简体   繁体   English

Java命令行包括.jar

[英]java commandline including .jar

I am able to use: 我可以使用:

C:\Program Files (x86)\Java>javac -classpath mail.jar myFile.java

to compile,but when I try to run it: 进行编译,但是当我尝试运行它时:

C:\Program Files (x86)\Java>java -cp mail.jar myFile
Exception in thread "main" java.lang.NoClassDefFoundError: myFile
Caused by: java.lang.ClassNotFoundException: myFile
    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)
Could not find the main class: myFile.  Program will exit.

I tried using various combinations using "." 我尝试使用“。”使用各种组合。 and "-classpath" to denote class path as other posts have sugested but couldn't figure it out. 和“ -classpath”表示班级路径,因为其他帖子已被引用,但无法弄清楚。 I've compiled and successfully run a basic helloworld.java in the same folder. 我已经在同一文件夹中编译并成功运行了基本的helloworld.java。 running as admin. 以管理员身份运行。 using java runtime 1.6. 使用Java运行时1.6。

Put your current directory into the classpath as well: 也将当前目录放入类路径中:

java -cp .;mail.jar myFile

If that doesn't work, post myFile.java so we can see what might be going on. 如果这不起作用,请发布myFile.java以便我们了解可能发生了什么。 For example, is myFile in the default package or is there a package statement? 例如, myFile在默认软件包中,或者有package声明?

You need to do one of the following: 您需要执行以下操作之一:

  • pack the jar first (use the jar command) and use java -jar 首先打包jar(使用jar命令)并使用java -jar

  • not specify a classpath and just run your class file (standalone applications) 不指定类路径,而仅运行您的类文件(独立应用程序)

  • (if the jarfile is a library) you need to include the current directory in your classpath (如果jarfile是库),则需要在类路径中包括当前目录

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

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