0

I created the following class located in the MainJPrint.java file

import com.XXXXX.pdfPrint.PDFPrint;

public class MainJPrint
{
   public static void main(String[] args) 
    { 
        //System.out.println("Hello World!"); 
        print(".....");
    }   
    public static String print (final String url)
    {
        Object rc = AccessController.doPrivileged(new java.security.PrivilegedAction() 
        {
            public Object run()
            {
                 ...
            }
         }
    }
}

In the same folder I have a jar archive jPrint.jar I compile the class using the following command

>javac -classpath jPrint.jar MainJPrint.java

When I'm trying to execute resulted class file, I get this error:

>java MainJPrint    

 java.lang.NoClassDefFoundError: com/XXXXX/pdfPrint/PDFPrint

If I uncomment the Hello World line and comment the next line, the program runs fine.

I'm using j2sdk1.4.2 installed at C:\j2sdk1.4.2. I do also have installed other java versions (at C:\Program Files\Java: jre 1.6.0_01, jre 1.6.0_02, j2re1.4.2, jre6, jre7, jdk1.7.0_03)

The PATH variable contains the C:\j2sdk1.4.2\bin path, however I think the java.exe is loaded from the upper version, but it shouldn't matter and I can call it like

>C:\j2sdk1.4.2\bin\java.exe MainJPrint

jPrint.jar is a third party archive and I need to create an applet which exposes a method so I can call it with javascript. I'm not a java developer, I'm having some little troubles and I'm really on an end here.

I tried other options like:

>java MainJPrint -cp .
>java MainJPrint -cp jPrint.jar

So how can I execute that class file which uses a class located in a separate archive?

6
  • Did you try java -cp jPrint.jar MainJPrint? The class argument is usually placed last. Commented May 4, 2012 at 12:03
  • java -cp jPring.jar MainJPrint should have worked. Commented May 4, 2012 at 12:03
  • Could you provide the whole MainJPrint.java content ? Commented May 4, 2012 at 12:05
  • Why would you still use j2sdk1.4.2? Commented May 4, 2012 at 12:11
  • @TimBüthe, the third party company recommends to create that applet using this version. Commented May 4, 2012 at 12:20

1 Answer 1

1

To execute a class that depends on external JARs, you need to specify all elements of the classpath on the command line.

If you don't specify a classpath, Java automatically uses . (the current directory), which is why, if MainJPrint didn't depend on jPrint.jar, your invocation java MainJPrint would have worked.

But when you specify -cp jPrint.jar, Java does NOT automatically add the current directory to the classpath, which means that it then cannot find MainJPrint. You need to specify both. On Mac/*nix, the following invocation should work:

java -cp jPrint.jar:. MainJPrint

Or on Windows:

java -cp jPrint.jar;. MainJPrint
Sign up to request clarification or add additional context in comments.

6 Comments

I'm getting Error: Could not find or load main class MainJPrint
MainJPrint is my class, is not located in the jar, that jar is a third party one
Are you certain that you're executing that exact command from within a folder that contains both jPrint.jar and the compiled MainJPrint.class files? You could try reversing the order of the classpath: java -cp .:jPrint.jar MainJPrint in case there's some quirk of circumstance going on...
This is unexpected behavior, and I've just been able to verify the accuracy of my suggestion with several versions of Java on a couple of platforms. For thoroughness, can you edit your question to include the output of each of the following commands, executed in sequence (assuming you're on Windows based on the > prompt): dir, javac -cp jPrint.jar MainJPrint.java, dir, java -cp . MainJPrint, java -cp jPrint.jar:. MainJPrint. It would also be helpful if, instead of an excerpt, you could edit MainJPrint to be a minimal example and post its complete code.
Ah, if you are on Windows, then you should actually be separating the entries with a semicolon instead of a colon: java -cp jPrint.jar;. MainJPrint. It's been so many years since I've used Windows that I completely forgot.
|

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.