简体   繁体   中英

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

After reading this question on adding dependencies through the command line, I'm unable to launch my application due to the following error:

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

I have tried the variations described in this question:

$ 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

where kiaragen is the root directory of the project(I need to run the kiaragen.class from outside that directory). I'm using 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)

on ubuntu 14.04. The bytecode file kiaragen.class exists and does have an entry point. The project has been compiled with IntelliJ IDEA 14.1.4.

The dependencies shown in IntelliJ are:

kiaragen_dependencies

What am I missing?

Edit #1:

I've run the following commands:

$ 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 is at the specified location:

$ 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

Why can't java find it?

Edit #2:

The packages are structured as:

package_tree

Edit #3:

kiaragen.java does have a main() method:

/*
     * ----------------------------------------------------------------------------------------
     * 
     * 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);
    }

The way you provide the path to your class in the java command is incorrect. But before we get to that, ensure that kiaragen has a public static void main(String []args) method.

If it does, try the following suggestion :

The fully qualified name of a Java class starts with the package and ends with the name of the class .

Assuming that the package statement in kiaragen.java is package org.fiware.kiara.generator; , the fully qualified name of this class is org.fiware.kiara.generator.kiaragen .

You can run this class from outside the root directory of your project as follows :

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

That being said, there is no need to add the jre/lib directory to the classpath explicitly unless you have placed some of your user defined jar files there.

First of all, you need to include the path to your class files (in the bin directory) in the classpath. Second, since judging from the IntelliJ screenshot src/main/ java is your source directory (it is marked blue), the fully qualified reference to your main class is org.fiware.kiara.generator.kiaragen .

Assuming you are in the parent directory of your program, try running

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

I've managed to get it running(by copying the run command from 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

To run without 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

Since no sane person wants to input that everytime I created an alias:

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

save and close and load .bashrc .

$ source ~/.bashrc

To execute the public static void main(String args[]); method of class org.abctheClass , which is located at d/e subdirectory, you have to execute the following statement:

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

You have to include the directory path that does not belong to the package name in the class path, and the path that belongs to the package name into the package name, so there must be a class file located at: d/e/org/a/b/c/theClass (file must be named d/e/org/a/b/c/theClass.class ).

For your case, it depends where are you trying to execute your java file from, but supposing you try to execute it from the same place where you executed the find command:

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

would be the proper command (you have never used absolute paths in your qestion, and no idea where your project directory is, so I used all paths relative to where you issued the find command)

So, in case you live at /home/yourname and supposing your kiaragen project is at workspace/kiaragen , a possible absolute path (allowing you to execute it independent of your current dir) would be:

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

(the $CLASSPATH allows you to include the original class path and to find the normal java library classes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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