1

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.

3
  • Did you try using wildcards along with it? Commented Aug 25, 2015 at 15:11
  • @XOR No I haven't, could you give me a code sample of what you mean exactly? Commented Aug 25, 2015 at 15:17
  • 1
    When you specify the -classpath parameter, it overrides the entire classpath, which means that the current directory . isn't included anymore the way you're doing it. Commented Aug 25, 2015 at 15:19

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

6 Comments

this answer is OS -specific, e.g. it will fail on Windows, see here stackoverflow.com/questions/4528438/…
@SergeyPauk is correct, it gives the same error as the original command
if you are on Windows then replace the colon with a semicolon in the answer above
changing the colon to a semi colon fixed it
@lscoughlin it's not about being pedantic but about the completeness of a given answer (as it turned out the author is indeed an unfortunate windows user :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.