rj93
rj93

Reputation: 573

Error when using java -classpath command

I have a class, test, as follows

import java.io.File;
import java.io.IOException;

import org.ini4j.InvalidFileFormatException;
import org.ini4j.Wini;

public class test {

    public static void main(String[] args) throws InvalidFileFormatException, IOException{
        File f = new File("ini.ini");
        Wini ini = new Wini(f);
        int a = ini.get("test", "a", int.class);
        int b = ini.get("test", "b", int.class);
        int c = ini.get("test", "c", int.class);

        System.out.print(a);
        System.out.print(b);
        System.out.print(c);
    }
}

To compile I run the command

javac -classpath ini4j-0.5.4.jar test.java

which compiles without issue, but when I try to run it with the command

java -classpath ini4j-0.5.4.jar test

I get the error Error: Could not find or load main class test.

If I exclude the -classpath ini4j-0.5.4.jar from the java .. test command I get an error Exception in thread "main" java.lang.NoClassDefFoundError: org/ini4j/InvalidFileFormatException ...

The class runs fine in eclipse.

Upvotes: 1

Views: 412

Answers (1)

lscoughlin
lscoughlin

Reputation: 2416

You need to add the directory your compiled class file ends up into your classpath in your java invocation.

In your case

java -cp "ini4j-0.5.4.jar:." test

would be what you want, since everything is in the current working directory.

Edit: Updated for completeness ^^. The class path uses a system specific path separator. So for the unfortunate souls using windows it would be:

java -cp "ini4j-0.5.4.jar;." test

Upvotes: 5

Related Questions