Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Command Line Trouble with Reading a Class from a Jar Archive

I am trying to run a java based tool using a command line syntax as the following: java -cp archive.jar archiveFolder.theMainClassName.Although the class I am searching for, a main class, "theMainClassName" is in the archive.jar and in the archiveFolder given at input, I keep getting the error that my class is not seen. Does anybody have any ideas concerning this problem? Thank you in advance

like image 917
acostache Avatar asked Nov 18 '25 22:11

acostache


1 Answers

Here's a concrete example of what does work, so you can compare your own situation.

Take this code and put it anywhere, in a file called MainClass.java. (I've assumed a directory called src later. Normally you'd arrange the source to match the package, of course.)

package archiveFolder;

public class MainClass
{
    public static void main(String[] args)
    {
        System.out.println("I'm MainClass");
    }
}

Then run each of these commands:

# Compile the source
javac -d . src/MainClass.java

# Build the jar file
jar cf archive.jar archiveFolder

# Remove the unpackaged binary, to prove it's not being used
rm -rf archiveFolder # Or rmdir /s /q archiveFolder on Windows

# Execute the class
java -cp archive.jar achiveFolder.MainClass

The result:

I'm MainClass

How are you building your jar file? Is the code in the appropriate package?

like image 72
Jon Skeet Avatar answered Nov 21 '25 10:11

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!