PKCS#11 library not found on 64bit Windows - java

In my job I have a requirement to sign XML file with certfifcate from SmartCard. Everything works fine but on 32bit Windows 7 VM.
The problems occur when I try to use the same code on 64bit Windows 10. I get error that "PKCS#11 not found" and "no such algorithm".
I use xades4j package. I've tried probably all solutions found in the web i.e.
Used different .dll libraries (pkcs11.dll, opensc-spy.dll, CPPkiP.dll, esp2003csp11.dll) in both 32 and 64 bits versions.
Placed .dll in both System32 and SysWOW64 directories.
Updated drivers for my reader (IDBridge CT30).
Changed jre to 32bit.
Used solutions from PKCS11 Reference Guide (without xades4j) - it also worked only on 32 bit.
Nothing helps and always the same exceptions occur. If someone have any ideas what else I could do, I will be really thankful for tips.
Best regards.
Caused by: java.security.KeyStoreException: PKCS11 not found
at java.security.KeyStore.getInstance(Unknown Source)
at java.security.KeyStore$Builder$2$1.run(Unknown Source)
at java.security.KeyStore$Builder$2$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 7 more
Caused by: java.security.NoSuchAlgorithmException: no such algorithm: PKCS11 for provider SunPKCS11-SmartCard
at sun.security.jca.GetInstance.getService(Unknown Source)
at sun.security.jca.GetInstance.getInstance(Unknown Source)
at java.security.Security.getImpl(Unknown Source)
... 11 more

I found library which solved my problem. It's https://github.com/esig/dss. I used JavaSE11(jdk-11.0.2), pkcs11.dll placed in C:/Windows/System32 directory.
Before run application it is necessary to add VM argument -Djava.security.debug=sunpkcs11,pkcs11

Related

How should I compile my java project targeting Java SE 8?

I have a Java Project (an application to play connect 4) and I want to turn it into an executable file. I created this project in VsCode, and I am using the Java Language Server (see details below). I extracted the .jar file, and used Launch4J to turn it into an executable, and when I run it, I get an error message (see below).
I researched the problem, and it seems that I need to compile my code targeting Java SE 8 rather than 17 (I have 17 currently installed). Could anyone explain to me how I should do this, or is there anything else I'm doing wrong?
The project has multiple files. I'm not sure if this is relevant, but the project has no external dependencies, although it does use javax.swing, javax.sound, and java.awt (as well as java.io.File, java.io.IOException, and java.util.Random, but I'm pretty sure those are irrelevant).
I have eclipse installed if it's necessary, but I only installed it today, and have no experience using it at all. The JDK I am using is (I believe) version 1.8.0_321. (At least this is what I get when I type in java -version.)
The Java compiler version is javac 17.0.2
When I try to run the executable generated by Launch4J, I this is the error I get:
Exception in thread "main" java.lang.UnsupportedClassVersionError: Main has been compiled by a more recent version of the Java Runtime (class file version 61.65535), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
And the popup error:
Java Virtual Machine Launcher
Error: A JNI error has occurred, please check your installation and try again.
Here are the Settings I used in Launch4J:
Output File: C:\Users\username\OneDrive\Coding\Java Connect 4 v.2\Connect 4.exe
Jar: C:\Users\username\OneDrive\Coding\Java\Java Connect 4 v.2\Java Connect 4 v.2.jar
Don't wrap the jar, launch only: NOT ticked
Wrapper Manifest: none
Change dir: .
Command Line args: none
Process priority: Normal
Both Options NOT ticked
Min JRE version: 1.2.0, Prefer public JRE, but use JDK runtime if newer
Max JRE version: none, First 64-bit, then 32-bit. (This was the default.)
All other settings are default.
Download the latest Java JDK version as .exe file (“x64 Installer”) and install by clicking on the .jar file.
Now the server should start again
#Holger has answered this question. The answer to the problem was to ship a custom JRE image with the application with the help of JLink. That way it can be used regardless of the local version of JRE.

NoSuchMethodError: <init> in com.sun.glass.ui.win.WinApplication.staticScreen_getScreens

Since upgrading to install4j 7.0.5 and Java 10, users that run our application on Windows more and more often report that the application throws
java.lang.NoSuchMethodError: <init>
at javafx.graphics/com.sun.glass.ui.win.WinApplication.staticScreen_getScreens(Native Method)
at javafx.graphics/com.sun.glass.ui.Screen.initScreens(Unknown Source)
at javafx.graphics/com.sun.glass.ui.Application.lambda$run$1(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
UiLauncher (WAITING)
at java.base#10.0.1/jdk.internal.misc.Unsafe.park(Native Method)
at java.base#10.0.1/java.util.concurrent.locks.LockSupport.park(Unknown Source)
at java.base#10.0.1/java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(Unknown Source)
at java.base#10.0.1/java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(Unknown Source)
at java.base#10.0.1/java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(Unknown Source)
at java.base#10.0.1/java.util.concurrent.CountDownLatch.await(Unknown Source)
at platform/javafx.graphics#10.0.1/com.sun.javafx.tk.quantum.QuantumToolkit.startup(Unknown Source)
at platform/javafx.graphics#10.0.1/com.sun.javafx.application.PlatformImpl.startup(Unknown Source)
at platform/javafx.graphics#10.0.1/com.sun.javafx.application.PlatformImpl.startup(Unknown Source)
at platform/javafx.swing#10.0.1/javafx.embed.swing.JFXPanel.initFx(Unknown Source)
at platform/javafx.swing#10.0.1/javafx.embed.swing.JFXPanel.<init>(Unknown Source)
at java.base#10.0.1/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base#10.0.1/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at java.base#10.0.1/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.base#10.0.1/java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.base#10.0.1/java.lang.Class.newInstance(Unknown Source)
at app//...
when starting the application though the install4j created exe file. The error is triggered by creating an instance of javafx.embed.swing.JFXPanel through reflection:
Class.forName("javafx.embed.swing.JFXPanel").newInstance();
We currently suspect that for some reason an incompatible DLL is loaded (glass.dll seems to contain the native method mentioned in the stacktrace).
Does anyone know how to prevent this error? E.g. is there are way to restrict the java.library.path used when executing the application through the install4j-generated exe to the Java runtime environment that was embedded in the installer and installed locally with the application?
According to one user, the error does not occur if the application is started "manually" using the
java -jar app.jar
command. So it seems the problem lies with the install4j created executable.
The workaround to this problem seems to be to remove all occurences of "glass.dll" from your system %PATH%.
I believe a fix must be implemented somewhere else, though; either in the Java runtime or in the Install4j code, but cannot be implemented in the Java code of the actual app:
For some reason the Java runtime version in Install4j checks the bundled JRE last when locating the libraries. In this case the problematic native library is glass.dll which should contain the requested <init> method but if anywhere in your %PATH% there is an older, incompatible version of glass.dll (e.g. from a previous Java 8 installation) that file will be loaded with a higher precedence and then the application will crash natively.
This is not a problem in the code of the application (the java code), nor a problem with the bundled JDK, this is a problem of how the install-4j-generated exe files (or maybe Java internally) tries to resolve native dlls. Instead of checking all path elements first, it should be checking the bundled JRE directory, first.
With Procmon you can see that it loads arbitrarily placed glass.dll files in the path, first: I added one from JDK 1.8 into one of my path elements and got this (plus the crash):

From where does the "Contains non-LDH ASCII characters" exception come from?

I develop an application (spring-magnolia) that around this new year (2018) started to throw for me this exception but not for any other colleagues.
The exception is right, there is an _ in the a configured host name, so that domain name should be fixed. Still, it was working before and for the other folks it is still working, not throwing this exception. I tried many different things, from tomcat settings to using an older JRE (1.7)... and even switched from Ubuntu to Windows but the exception wasn't left behind.
One of my mates is working with me in the office, using Mac, and I could look up his settings to catch what is making this difference, but don't really know what else should I look up as the tools, environment are the same (maven, same code, tried different jdks).
Could someone give me a clue what triggered the exception for me but not for others and if I could set it somewhere to ignore this exception?
Caused by: java.security.cert.CertificateException: Illegal given domain name: xyz_t-bud.rd-services.com
at sun.security.util.HostnameChecker.matchDNS(Unknown Source)
at sun.security.util.HostnameChecker.match(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkIdentity(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkIdentity(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
... 135 more
Caused by: java.lang.IllegalArgumentException: Contains non-LDH ASCII characters
at java.net.IDN.toASCIIInternal(Unknown Source)
at java.net.IDN.toASCII(Unknown Source)
at javax.net.ssl.SNIHostName.<init>(Unknown Source)
... 141 more
Apparently I wasn't testing with older JDKs, as eclipse wasn't picking it up. After uninstalling the latest JDK 1.8.0_161and installing the JDK that my colleague is having, that is 1.8.0_111 I am no longer getting this error... I did try this JDK before, but I didn't uninstall the latest JDK and so the project was in fact still using the latest JDK...

Issues registering Sqlite driver in Java

I am trying to connect to a sqlite database from Java. I am programming in Windows. I downloaded the sqlite driver and set the classpath in the windows environment variables. I get a classNotFoundException when this code is called:
Class.forName(sDriver);
where sDriver is "org.sqlite.JDBC" Here is the stacktrace from the exception:
java.lang.ClassNotFoundException: org.sqlite.JDBC
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at com.ib.client.examples.Mydb.setConnection(Mydb.java:49)
at com.ib.client.examples.Mydb.init(Mydb.java:33)
at com.ib.client.examples.Mydb.<init>(Mydb.java:17)
at com.ib.client.examples.Example1.run(Example1.java:40)
Any help would be greatly appreciated. Thanks.
I don't know which JDBC driver you are trying to use, but the best SQLite JDBC driver i found so far is the one made by the Xerial Project:
http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC
You can find the latest version of the driver here:
http://www.xerial.org/maven/repository/snapshot/org/xerial/sqlite-jdbc/3.7.8-SNAPSHOT/
It uses a native driver for Windows (32 and 64 bits), Linux (32 and 64 bits) and MacOS (32 and 64 bits), and falls back to a pure java implementation in the case of an unsupported platform.
All native libraries (dll, so, etc) are included in the driver's JAR file, and you don't need to worry about loading any of them. It will autodetect the right library for you.
EDIT:
If you are trying to run your program from command line, you must specify every jar file in the classpath. Setting their path in the classpath environment variable is not enough.
Example:
java YourProgram -cp classes;lib/sqlite.jar;c:\libs\java\lib1.jar

Error while launching my java application(swt) on Mac

I am developing a java application on windows 7 (32 bit) machine. I used SWT in my application and run it by Java Web Start. It is working fine on windows 7 but when I try to run it on MAC OSX 10.6.8 it is unable to launch my application. I have added "swt-3.7M5-win32-win32-x86.jar" in my library.
I added the following line in my jnlp file to run it on 32 bit JVM:
<j2se version="1.6+" java-vm-args="-d32" href="http://java.sun.com/products/autodl/j2se"/>
but still it is not working.
It is throwing the following error:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.javaws.Launcher.executeApplication(Launcher.java:1914)
at com.sun.javaws.Launcher.executeMainClass(Launcher.java:1847)
at com.sun.javaws.Launcher.doLaunchApp(Launcher.java:1609)
at com.sun.javaws.Launcher.run(Launcher.java:138)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons:
no swt-win32-3721 in java.library.path
no swt-win32 in java.library.path
Can't load library: /Users/Rajivj/.swt/lib/macosx/x86/libswt-win32-3721.jnilib
Can't load library: /Users/Rajivj/.swt/lib/macosx/x86/libswt-win32.jnilib
at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
at org.eclipse.swt.internal.C.<clinit>(Unknown Source)
at org.eclipse.swt.widgets.Display.<clinit>(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.eclipse.swt.graphics.Device.<clinit>(Unknown Source)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeInterface$InProcess.initialize(SWTNativeInterface.java:559)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeInterface$InProcess.access$2(SWTNativeInterface.java:558)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeInterface.initialize_(SWTNativeInterface.java:238)
at chrriis.dj.nativeswing.swtimpl.NativeInterface.initialize(NativeInterface.java:71)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeInterface.open_(SWTNativeInterface.java:316)
at chrriis.dj.nativeswing.swtimpl.NativeInterface.open(NativeInterface.java:100)
at rtlmatrixcloud.Main.main(Main.java:32)
... 9 more
Can anyone tell me How do I solve this problem?
Any help would be appreciated.
Thanks
Sonal
This is the root problem:
Caused by: java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons:
no swt-win32-3721 in java.library.path
no swt-win32 in java.library.path
Can't load library: /Users/Rajivj/.swt/lib/macosx/x86/libswt-win32-3721.jnilib
Can't load library: /Users/Rajivj/.swt/lib/macosx/x86/libswt-win32.jnilib
First check that these lib files are there. I believe they are because it says "UnsatisfiedLinkError" and does not complain that file is not found. In this case check your SWT installation. It seems these libraries do not match your JVM. Typically such exception is thrown when for example 32 bit JVM tries to invoke 64 bit library or vice versa.

Categories

Resources