当通过命令行 运行 时无法找到或加载主 class

Could not find or load main class when running through command line

阅读 this 关于通过命令行添加依赖项的问题后,由于以下错误,我无法启动我的应用程序:

Error: Could not find or load main class kiaragen.class

我已经尝试了 this 问题中描述的变体:

$ java -cp ".:/usr/lib/jvm/java-8-jdk/jre/lib/*:kiaragen/lib/*" kiaragen/bin/org/fiware/kiara/generator/kiaragen.class
Error: Could not find or load main class kiaragen.bin.org.fiware.kiara.generator.kiaragen.class
$ java -cp ".:/usr/lib/jvm/java-8-jdk/jre/lib/*:kiaragen/lib/*" kiaragen/bin/org/fiware/kiara/generator/kiaragen
Error: Could not find or load main class kiaragen.bin.org.fiware.kiara.generator.kiaragen
$ java -cp .:/usr/lib/jvm/java-8-jdk/jre/lib/*:kiaragen/lib/* kiaragen/bin/org/fiware/kiara/generator/kiaragen.class
Error: Could not find or load main class kiaragen.bin.org.fiware.kiara.generator.kiaragen.class
$ java -cp .:/usr/lib/jvm/java-8-jdk/jre/lib/*:kiaragen/lib/* kiaragen/bin/org/fiware/kiara/generator/kiaragen
Error: Could not find or load main class kiaragen.bin.org.fiware.kiara.generator.kiaragen
$ java -classpath ".:/usr/lib/jvm/java-8-jdk/jre/lib/*:kiaragen/lib/*" kiaragen/bin/org/fiware/kiara/generator/kiaragen
Error: Could not find or load main class kiaragen.bin.org.fiware.kiara.generator.kiaragen
$ java -classpath .:/usr/lib/jvm/java-8-jdk/jre/lib/*:kiaragen/lib/* kiaragen/bin/org/fiware/kiara/generator/kiaragen
Error: Could not find or load main class kiaragen.bin.org.fiware.kiara.generator.kiaragen

其中 kiaragen 是项目的根目录(我需要 运行 kiaragen.class 来自在该目录之外)。我正在使用 java 1.8:

$ java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

于 ubuntu 14.04。字节码文件 kiaragen.class 存在并且确实有一个入口点。该项目已使用 IntelliJ IDEA 14.1.4 编译。

IntelliJ 中显示的依赖项是:

我错过了什么?

编辑#1:

我已经运行执行了以下命令:

$ java -cp ".:/usr/lib/jvm/java-8-jdk/jre/lib/*:/home/kiara/AppLab/KIARA/kiaragen/lib/*" kiaragen/bin/org.fiware.kiara.generator.kiaragen
Error: Could not find or load main class kiaragen.bin.org.fiware.kiara.generator.kiaragen
$ java -cp ".:kiaragen/lib/*" kiaragen/bin/org.fiware.kiara.generator.kiaragen
Error: Could not find or load main class kiaragen.bin.org.fiware.kiara.generator.kiaragen
$ java -cp ".:kiaragen/lib/*" kiaragen/bin/org.fiware.kiara.generator.kiaragen
Error: Could not find or load main class kiaragen.bin.org.fiware.kiara.generator.kiaragen

kiaragen.class在指定位置:

$ find kiaragen/ -name "kiaragen*"
kiaragen/
kiaragen/kiaragen.iml
kiaragen/src/main/java/org/fiware/kiara/generator/kiaragen.java
kiaragen/bin/org/fiware/kiara/generator/kiaragen$TemplateErrorListener.class
kiaragen/bin/org/fiware/kiara/generator/kiaragen.class
kiaragen/scripts/kiaragen.sh
kiaragen/scripts/kiaragen.bat

为什么java找不到?

编辑#2:

包的结构如下:

编辑#3:

kiaragen.java 确实有一个 main() 方法:

/*
     * ----------------------------------------------------------------------------------------
     * 
     * Main entry point
     */

    public static void main(String[] args) {
        ColorMessage.load();

        m_platforms = new ArrayList<String>();
        m_platforms.add("gradle");

        try {

            kiaragen main = new kiaragen(args);
            if (main.execute()) {
                System.exit(0);
            }

        } catch (BadArgumentException e) {

            System.out.println(ColorMessage.error("BadArgumentException") + e.getMessage());
            printHelp();

        }

        System.exit(-1);
    }

您在 java 命令中提供 class 路径的方式不正确。但在开始之前,请确保 kiaragen 有一个 public static void main(String []args) 方法。

如果是,请尝试以下建议:

Java class 的完全限定名称以 package 开始,以 class.

的名称结束

假设kiaragen.java中的包语句是package org.fiware.kiara.generator;,这个class的全限定名是org.fiware.kiara.generator.kiaragen

您可以 运行 这个 class 从项目的根目录之外,如下所示:

java -cp ".:/usr/lib/jvm/java-8-jdk/jre/lib/*:kiaragen/lib/*" kiaragen/bin/org.fiware.kiara.generator.kiaragen

也就是说,没有必要将 jre/lib 目录显式添加到 class 路径,除非您已将一些用户定义的 jar 文件放在那里。

首先,您需要在 class 路径中包含 class 文件(在 bin 目录中)的路径。其次,由于从 IntelliJ 屏幕截图判断 src/main/java 是您的源目录(标记为蓝色),因此对您的 main class 是 org.fiware.kiara.generator.kiaragen

假设您在程序的父目录中,请尝试 运行

java -cp kiaragen/lib/*:kiaragen/bin org.fiware.kiara.generator.kiaragen

我已经设法 运行ning(通过从 IntelliJ 复制 运行 命令):

$ /usr/lib/jvm/java-8-jdk/bin/java -Didea.launcher.port=7534 -Didea.launcher.bin.path=/usr/share/intellijidea-ce/bin -Dfile.encoding=UTF-8 -classpath /home/kiara/AppLab/KIARA/kiaragen/bin:/usr/lib/jvm/java-8-jdk/jre/lib/jsse.jar:/usr/lib/jvm/java-8-jdk/jre/lib/plugin.jar:/usr/lib/jvm/java-8-jdk/jre/lib/jfr.jar:/usr/lib/jvm/java-8-jdk/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-jdk/jre/lib/charsets.jar:/usr/lib/jvm/java-8-jdk/jre/lib/rt.jar:/usr/lib/jvm/java-8-jdk/jre/lib/deploy.jar:/usr/lib/jvm/java-8-jdk/jre/lib/javaws.jar:/usr/lib/jvm/java-8-jdk/jre/lib/resources.jar:/usr/lib/jvm/java-8-jdk/jre/lib/jce.jar:/usr/lib/jvm/java-8-jdk/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/localedata.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/antxr.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/stringtemplate-3.2.1.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/kiaraparser-0.1.0.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/antlr-4.4-complete.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/antlr-2.7.7.jar:/usr/share/intellijidea-ce/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain org.fiware.kiara.generator.kiaragen

没有 IntelliJ 的 运行:

$ /usr/lib/jvm/java-8-jdk/bin/java -classpath /home/kiara/AppLab/KIARA/kiaragen/bin:/usr/lib/jvm/java-8-jdk/jre/lib/jsse.jar:/usr/lib/jvm/java-8-jdk/jre/lib/plugin.jar:/usr/lib/jvm/java-8-jdk/jre/lib/jfr.jar:/usr/lib/jvm/java-8-jdk/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-jdk/jre/lib/charsets.jar:/usr/lib/jvm/java-8-jdk/jre/lib/rt.jar:/usr/lib/jvm/java-8-jdk/jre/lib/deploy.jar:/usr/lib/jvm/java-8-jdk/jre/lib/javaws.jar:/usr/lib/jvm/java-8-jdk/jre/lib/resources.jar:/usr/lib/jvm/java-8-jdk/jre/lib/jce.jar:/usr/lib/jvm/java-8-jdk/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/localedata.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/antxr.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/stringtemplate-3.2.1.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/kiaraparser-0.1.0.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/antlr-4.4-complete.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/antlr-2.7.7.jar:/usr/share/intellijidea-ce/lib/idea_rt.jar  org.fiware.kiara.generator.kiaragen

因为没有正常人愿意在我每次创建别名时输入:

$ vim ~/.bashrc
alias kiaragen="/usr/lib/jvm/java-8-jdk/bin/java -classpath /home/kiara/AppLab/KIARA/kiaragen/bin:/usr/lib/jvm/java-8-jdk/jre/lib/jsse.jar:/usr/lib/jvm/java-8-jdk/jre/lib/plugin.jar:/usr/lib/jvm/java-8-jdk/jre/lib/jfr.jar:/usr/lib/jvm/java-8-jdk/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-jdk/jre/lib/charsets.jar:/usr/lib/jvm/java-8-jdk/jre/lib/rt.jar:/usr/lib/jvm/java-8-jdk/jre/lib/deploy.jar:/usr/lib/jvm/java-8-jdk/jre/lib/javaws.jar:/usr/lib/jvm/java-8-jdk/jre/lib/resources.jar:/usr/lib/jvm/java-8-jdk/jre/lib/jce.jar:/usr/lib/jvm/java-8-jdk/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-jdk/jre/lib/ext/localedata.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/antxr.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/stringtemplate-3.2.1.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/kiaraparser-0.1.0.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/antlr-4.4-complete.jar:/home/kiara/AppLab/KIARA/kiaragen/lib/antlr-2.7.7.jar org.fiware.kiara.generator.kiaragen"

保存并关闭并加载.bashrc.

$ source ~/.bashrc

要执行 class org.a.b.c.theClasspublic static void main(String args[]); 方法,它位于 d/e 子目录,您必须执行以下语句:

java -cp 'd/e:$CLASSPATH' org.a.b.c.theClass

你必须在class路径中包含属于包名的目录路径,和属于把包名改成包名,所以一定有一个class文件位于:d/e/org/a/b/c/theClass(文件名必须是d/e/org/a/b/c/theClass.class).

对于您的情况,这取决于您尝试从何处执行 java 文件,但假设您尝试从执行 find 命令的同一位置执行它:

java -cp "kiaragen/bin:$CLASSPATH" org.fiware.kiara.generator.kiaragen

将是正确的命令(您从未在问题中使用过绝对路径,也不知道您的项目目录在哪里,所以我使用了相对于您发出 find 命令的位置的所有路径)

因此,如果您住在 /home/yourname 并且假设您的 kiaragen 项目位于 workspace/kiaragen,一个可能的绝对路径(允许您独立于当前目录执行它)将是:

java -cp "/home/yourname/workspace/kiaragen/bin:$CLASSPATH" org.fiware.iara.generator.kiaragen

$CLASSPATH 允许您包含原始 class 路径并找到正常的 java 库 classes)