adifire
adifire

Reputation: 610

NoClassDefFoundError while running a java program in terminal from IDE

I am having trouble running a java program that works fine in the IntelliJ IDEA ide. The error I get when I run the same code (after removing the package ..) as follows

Exception in thread "main" java.lang.NoClassDefFoundError: fcrypt
Caused by: java.lang.ClassNotFoundException: fcrypt
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

All I'm doing in the main method is creating an instance of the main class and calling the several methods. The code with just the headers and the main method as below

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;

/**
 * Created by Aditya Rao on 05/02/14.
 */
public class fcrypt {
    private static final String RSA_NONE_PKCS1PADDING = "RSA/None/PKCS1Padding";

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    ....

    public static void main (String[] args) throws Exception {
        if (args.length != 5) {
            System.out.print("Invalid parameters. ");
            printUsage();
            System.exit(1);
        }

        if (!(args[0].equals("-e") | args[0].equals("-d"))) {
            System.out.print("Please specify usage. ");
            printUsage();
            System.exit(1);
        }

        fcrypt f = new fcrypt();

        String[] inputs = Arrays.copyOfRange(args, 1, args.length);
        if (args[0].equals("-e"))
            f.encryptAndSign(inputs);
        else
            f.verifyAndDecrypt(inputs);
    }
 }

Am I missing something here?

EDIT I compile and run this program with the following commands

javac -cp libs/bcprov-jdk15on-150.jar fcrypt.java
java -cp libs/bcprov-jdk15on-150.jar fcrypt <args>

Upvotes: 1

Views: 2148

Answers (1)

divanov
divanov

Reputation: 6339

You have to add working directory denoted as . to the class path as fcrypt.class is located there.

Syntax for Unix:

java -cp ".:libs/bcprov-jdk15on-150.jar" fcrypt

note elements are separated with :.

Syntax for Windows:

java -cp ".;libs/bcprov-jdk15on-150.jar" fcrypt

note elements are separated with ;.

Java code style suggests class names to start with a capital letter. So it should be class FCrypt defined in FCrypt.java.

Upvotes: 2

Related Questions