2

I have a java program, which works correctly inside IDE. However when i create jar, it does not work. What's the problem?

        StringBuilder builder = new StringBuilder();
    builder.append("name=" + name);
    builder.append(System.getProperty("line.separator"));
    builder.append("library=\"" + library + "\"");
    builder.append(System.getProperty("line.separator"));
    builder.append("slot=" + slot);

    ByteArrayInputStream bais = new ByteArrayInputStream(builder.toString().getBytes());
    /*exception line  */SunPKCS11 provider = new SunPKCS11(bais);


Exception in thread "main" java.lang.NoSuchMethodError: 'void sun.security.pkcs11.SunPKCS11.<init>(java.io.InputStream)'
at JcaProvider.createProvider(JcaProvider.java:70)
at Test.main(Test.java:31)

There is no error or warning when I create jar, Can someone help me?

Updating property file: /home/mehmet/works/NetbeansProjects/HSM_Java/build/built-clean.properties Deleting directory /home/mehmet/works/NetbeansProjects/HSM_Java/build

clean:

init:

deps-jar:

Created dir: /home/****/****/NetbeansProjects/HSM_Java/build

Updating property file: /home/****/***/NetbeansProjects/HSM_Java/build/built-jar.properties

Created dir: /home///NetbeansProjects/HSM_Java/build/classes

Created dir: /home///NetbeansProjects/HSM_Java/build/empty

Created dir: /home///NetbeansProjects/HSM_Java/build/generated-sources/ap-source-output

Compiling 5 source files to /home///NetbeansProjects/HSM_Java/build/classes

compile:

Created dir: /home///NetbeansProjects/HSM_Java/dist

Copying 1 file to /home/****/***/NetbeansProjects/HSM_Java/build

Nothing to copy.

Building jar: /home/****/***/NetbeansProjects/HSM_Java/dist/HSM_Java.jar

To run this application from the command line without Ant, try: java -jar "/home/****/***/NetbeansProjects/HSM_Java/dist/HSM_Java.jar"

Mehmet Özcan
  • 85
  • 1
  • 5

1 Answers1

3

NoSuchMethodErrors typically mean Version Mismatches. This can only happen when the compiler and the jvm that executes it, see different method signatures.

In your case, I suspect that java is a different jvm than the one supplied by Netbeans. Open the Netbeans folder and lookout for a java installation. Using that installation will probably owkr.

Note that sun.* packages are proprietary Sun API and thus it's very possible that it's not available in your java installation (e.g. oracle jvm vs openJDK jvm). Or you might be using post-java-8 jvm's, where they changed a few things (removed some classes out, which require enabling or others).

So in other words: If you have the chance, try to find replacements for sun packages.

MeFisto94
  • 157
  • 5
  • 1
    indeed, the InputStream constructor disappeared since Java 9 by the looks of it. And that seems to be because obtaining a Provider instance is [done differently](https://stackoverflow.com/questions/46521791/sunpkcs11-provider-in-java-9). – Gimby Dec 06 '19 at 16:02