1

First of all thank you for reading my question. I have a .jar file and .java file which I should compile

They are "fractals.jar" and "FractalsTest.java"

So basically "fractals.jar" contains .class files with packages to run a simple program that displays fractals on a java program, and the directory looks like this.

fractals -- FractalExplorer
          | FracalGenerator
          | JImageDisplay
          + generator -- Mandelbrot
                      -- BurningShip
                      -- Tricorn

So, The top three classes are under directory(folder) and package called "fractals" the the three fractal patterns under directory(folder_ and package named "fractals.generator" FractalGenerator utilizes the three fractal pattern classes by importing them, and FractalExplorer class is the class that contains the main function. I have succeeded in compiling the six classes and compressed them as "fractals.jar" file with manifest including "Main-Class: fractals.FractalExplorer". Eventually, the "fractals.jar" file runs perfectly fine. But my goal here is to make another java file that utilizes the "fractals.jar" file like a library to run the same thing!!! So here is the code of "FractalsTest.java"

import fractals.*;

public class FractalsTest
{
    public static void main(String[] args)
    {
        FractalExplorer fracExp = new FractalExplorer(800);
        fracExp.createAndShowGUI();
        fracExp.drawFractal();
    }
}

I tried compiling it by entering on the cmd like this:

>javac -classpath fractals.jar FractalsTest.java

(assuming they both are on desktop)

It compiles perfectly fine, and "FractalsTest.class" is created.

And now all I need to do is entering

java -classpath fractals.jar FractalsTest

BUT!!!! The console screen shows that "Could not find or load main class FractalsTest" with "java.lang.ClassNotFoundException: FractalsTest"

Please help!!!What mistakes have I done?????

Lee
  • 21
  • 4

1 Answers1

0

I guess java -classpath fractals.jar FractalsTest searches for FractalsTest.class inside fractals.jar. Simple java FractalsTest should do the trick.

Edit: This answer states that the current directory must be manually added to the classpath. So add :. at the end of your classpath to include the current directory.

Mathias
  • 282
  • 1
  • 16
  • 1
    C:\Users\H.G\Desktop>java FractalsTest Exception in thread "main" java.lang.NoClassDefFoundError: fractals/FractalExplorer at FractalsTest.main(FractalsTest.java:7) Caused by: java.lang.ClassNotFoundException: fractals.FractalExplorer at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ... 1 more – Lee Apr 30 '21 at 15:11
  • Sorry, I just had to learn something about the classpath myself. Thanks for the stacktrace, this lead me to the (hopefully correct) solution. – Mathias May 01 '21 at 19:54