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

时间:2015-07-12 13:03:31

标签: java linux intellij-idea command-line classpath

在阅读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中显示的依赖项是:

kiaragen_dependencies

我错过了什么?

编辑#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:

包的结构如下:

package_tree

编辑#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);
    }

4 个答案:

答案 0 :(得分:1)

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

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

Java class的完全限定名称以package开头,以class的名称结尾。

假设kiaragen.java中的包语句为package org.fiware.kiara.generator;,则此类的完全限定名称为org.fiware.kiara.generator.kiaragen

您可以从项目的根目录外部运行此类,如下所示:

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

话虽如此,除非您在那里放置了一些用户定义的jre/lib文件,否则无需将jar目录明确添加到类路径中。

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:0)

我设法让它运行(通过从IntelliJ复制run命令):

$ /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

答案 3 :(得分:0)

要执行位于public static void main(String args[]);子目录的类org.a.b.c.theClass的{​​{1}}方法,您必须执行以下语句:

d/e

您必须将所属的目录路径包含在类路径中,和属于路径名的路径包含在包名称中,因此必须有一个类文件位于:java -cp 'd/e:$CLASSPATH' org.a.b.c.theClass (文件必须命名为d/e/org/a/b/c/theClass)。

对于您的情况,它取决于您尝试执行java文件的位置,但假设您尝试从执行d/e/org/a/b/c/theClass.class命令的同一位置执行它:

find

将是正确的命令(你从来没有在你的qestion中使用绝对路径,也不知道你的项目目录在哪里,所以我使用了相对于你发出java -cp "kiaragen/bin:$CLASSPATH" org.fiware.kiara.generator.kiaragen 命令的地方的所有路径)

所以,如果你住在find并假设你的kiaragen项目在/home/yourname,可能的绝对路径(允许你独立于你当前的目录执行它)将是:

workspace/kiaragen

java -cp "/home/yourname/workspace/kiaragen/bin:$CLASSPATH" org.fiware.iara.generator.kiaragen 允许您包含原始类路径并查找普通的java库类)