NoClassDefFoundError while running a java program in terminal from IDE

蓝咒 提交于 2019-12-24 15:26:03

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/21655358/noclassdeffounderror-while-running-a-java-program-in-terminal-from-ide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!