1

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);
    }
Community
  • 1
  • 1
Sebi
  • 4,262
  • 13
  • 60
  • 116
  • What's the `package` for `kiaragen`? Which `jar` contains this `class`? – Chetan Kinger Jul 12 '15 at 13:06
  • @CKing "kiaragen.src.main.java.org.fiware.kiara.generator.kiaragen.java" – Sebi Jul 12 '15 at 13:08
  • kiaragen.class is the actual application I want to run. It is not part of a .jar file but depends on those mentioned in the question. – Sebi Jul 12 '15 at 13:14
  • See my answer and let me know if it works for you. I don't have enough information to point out the exact command to run your class but my answer will point you in the right direction. You have been around for a while so you know this site works. Feedback is important. – Chetan Kinger Jul 12 '15 at 13:32

4 Answers4

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.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
  • I've run all commands with the fully qualified names, even added the full path to the classpath, yet it still can't find kiarage.class. I've edited the question. – Sebi Jul 12 '15 at 13:34
  • @Sebi It's really not that complex if you understand the basics. I suggest you go through the oracle documentation on `CLASSPATH`. – Chetan Kinger Jul 12 '15 at 13:38
  • I do understand the basics. I do not understand why I java shows the error Error: Could not find or load main class regardless of the input(see the edits). – Sebi Jul 12 '15 at 13:41
  • @Sebi Well, does `kiaragen.java` have a `public static void main(String []args)` method? – Chetan Kinger Jul 12 '15 at 13:42
  • Yes, it does and it's unique within the project(Edit #3). – Sebi Jul 12 '15 at 13:46
1

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
hzpz
  • 7,536
  • 1
  • 38
  • 44
0

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
Sebi
  • 4,262
  • 13
  • 60
  • 116
  • 1) You are still including an IntelliJ IDEA lib in your classpath: `/usr/share/intellijidea-ce/lib/idea_rt.jar`. 2) I am not sure why IntelliJ IDEA chooses to explicitly include the JDK JARs. Another than that, I seems to use a verbose version of the one I suggested in my answer. Did that not work for you? – hzpz Jul 12 '15 at 15:20
  • @hzpz Thanks for the notice. Removed the entry and it still worked. I've tried your answer and it works as well. – Sebi Jul 12 '15 at 15:29
  • 1
    Don't mention it! If your problem is solved, please accept an answer. – hzpz Jul 13 '15 at 18:22
0

To execute the public static void main(String args[]); method of class org.a.b.c.theClass, 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)

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31