简体   繁体   English

Java类路径问题-无法运行

[英]Java Classpath Issues - will not run

I'm learning how to run Java from the command line, and keep running into the same issue. 我正在学习如何从命令行运行Java,并不断遇到相同的问题。 The main() method I'm running is in bin/edu/cuny/util/ConvertTestVectors.class . 我正在运行的main()方法位于bin/edu/cuny/util/ConvertTestVectors.class

I set my directory to bin/cuny/ . 我将目录设置为bin/cuny/ When I run > java -cp . ConvertTestVectors 当我运行> java -cp . ConvertTestVectors > java -cp . ConvertTestVectors I get: 我得到的> java -cp . ConvertTestVectors

Error: Could not find or load main class ConvertTestVectors

When I run > java -cp . util/ConvertTestVectors 当我运行> java -cp . util/ConvertTestVectors > java -cp . util/ConvertTestVectors I get: > java -cp . util/ConvertTestVectors我得到:

Exception in thread "main" java.lang.NoClassDefFoundError: util/ConvertTestVectors (wrong name: edu/cuny/util/ConvertTestVectors)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)

Could someone point me in the right direction? 有人可以指出我正确的方向吗? Thanks! 谢谢!

To run a main method of some java class you have to specify the fully qualified name of this class in the command line. 要运行某个Java类的main方法,您必须在命令行中指定此类的全限定名。 For example, if you have in your source: 例如,如果您的来源中有:

package edu.cuny.util;

class ConvertTestVectors {
    ...
    public static void main(String [] args) {
        ...
    }
    ...
}

then to run this main method you should use 然后要运行此main方法,您应该使用

java edu.cuny.util.ConvertTestVectors

If your package name is different, you should change the class name in command line accordingly. 如果软件包名称不同,则应在命令行中相应地更改类名称。

Also, there is a CLASSPATH variable, which determines the location where java looks for classes. 另外,还有一个CLASSPATH变量,该变量确定java查找类的位置。 It should contain the directory where the root of your java packages in located (the parent directory of edu is case of the previous example). 它应该包含您的Java包的根目录所在的目录( edu的父目录是上一示例的情况)。 If it contains . 如果包含. then you can just change directory to that root directory and run java command. 那么您可以将目录更改为该根目录并运行java命令。

The parts of a fully qualified class name are separated by dots ( . ) so you should not use slashes ( / ) in class name 完全合格的类名称的各部分之间用点号( . )分隔,因此您不应在类名称中使用斜杠( / )。

cd bin/edu/cuny && java util.ConvertTestVectors

您必须使用完整的软件包名称。

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

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