I trying to attach agent to self JVM. When I calling VirtualMachine#attach it throws this exception:
java.util.ServiceConfigurationError: com.sun.tools.attach.spi.AttachProvider: Provider sun.tools.attach.WindowsAttachProvider not a subtype
com.sun.tools.attach.AttachNotSupportedException: no providers installed
There is my code:
static {
System.load("FullPathToAttachDLL/attach.dll");
}
public static void main(final String[] args){
final VirtualMachine virtualMachine = VirtualMachine.attach("" + ImplUtils.getSelfPID());
virtualMachine.loadAgent(file.getAbsolutePath()); // file is my agent.jar
}
P.S I need to fix it at runtime
Related
I get the error by running this simple main method:
public class Test {
public static void main(String[] args) throws Exception {
byte[] result =
DatatypeConverter
.parseBase64Binary("some key");
}
}
The answers I've managed to find are back from 2014 and advise to use Java 1.6, while I use Java 11.
I'm trying to access a simple java code from inside my C# webapp using jni4net, but it is throwing some errors.
Generated all proxies and dlls to access the java class.
I wrote the code for connecting with JVM inside the 'Program.cs' file.
Later on the custom java function ie. display_msg() is called from the testfunc() which can be called from anywhere inside the bot using Program.testfunc().
I'm attaching the Program.cs file and the exception occurring.
Also I've named my java file as Test.java and it's inside package mypack.
Program.cs
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using net.sf.jni4net;
using System;
using mypack;
namespace ValidationBot
{
public class Program
{
public static void Main(string[] args)
{
var setup = new BridgeSetup();
setup.Verbose = true;
setup.AddAllJarsClassPath("./");
Bridge.CreateJVM(setup);
Bridge.RegisterAssembly(typeof(Test).Assembly);
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((logging) =>
{
logging.AddDebug();
logging.AddConsole();
}).UseStartup<Startup>();
public static void testfunc()
{
Test test = new Test();
test.display_msg();
Console.WriteLine("\nPress any key to quit.");
Console.ReadKey();
}
}
}
Test.java
package mypack;
import java.io.*;
public class Test
{
public static void main(String args[])
{
int s =10;
System.out.println(s);
}
public void display_msg()
{
System.out.println("Hello, I'm inside a java program");
}
}
Exception
Exception thrown: 'System.MissingMethodException' in jni4net.n-0.8.8.0.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in System.Private.CoreLib.dll
Exception thrown: 'System.TypeInitializationException' in jni4net.n-0.8.8.0.dll
An unhandled exception of type 'System.TypeInitializationException' occurred in jni4net.n-0.8.8.0.dll
The type initializer for 'net.sf.jni4net.utils.Registry' threw an exception.
I'm a beginner to C# so please help me out with this.
From the comments we deduced that .NET core 2.1 does not support this method: (and maybe others)
System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)
I have those java files:
//i2cjni.java
public class i2cJNI {
static {
try {
System.loadLibrary("i2cbrdg");
}
catch(UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
}
}
public final static native long U2C_GetDeviceCount();
public final static native long U2C_OpenDevice(long jarg1);
public final static native long U2C_CloseDevice(long jarg1);
...
}
//i2c.java
public class i2c {
public static SWIGTYPE_p_BYTE U2C_GetDeviceCount() {
return new SWIGTYPE_p_BYTE(i2cJNI.U2C_GetDeviceCount(), true);
}
//I2CBoard.java
public class I2CBoard extends i2c{
public static void main(String[] args) {
SWIGTYPE_p_BYTE nDevice = new SWIGTYPE_p_BYTE();
i2c.U2C_OpenDevice(nDevice);
SWIGTYPE_p_BYTE mDevice = i2c.U2C_GetDeviceCount();
System.out.println("mDevice: " + mDevice);
}
}
-I have set the native library path like this:
i2c_wrapper/Native library location: ro.i2c.mirela/os/x86_64
and in the same way to the rest of them: JRE System Library, Plug-in Dependencies, ro.i2c.mirela/os(class folder) and i2c_wrapper
-I have added this folder to the project:
os/x86_64/i2c_wrapper.dll
os/x86_64/i2cbrdg.dll
os/x86_64/U2CCommon.dll
-when I try to call the U2C_GetDeviceCount() function and so on, I get this error. Any ideas?
Exception in thread "main" java.lang.UnsatisfiedLinkError: ro.i2c.mirela.board.i2cJNI.U2C_OpenDevice(J)J
at ro.i2c.mirela.board.i2cJNI.U2C_OpenDevice(Native Method)
at ro.i2c.mirela.board.i2c.U2C_OpenDevice(i2c.java:28)
at ro.i2c.mirela.board.I2CBoard.main(I2CBoard.java:8)
that like is a link error,may be your .c file function name is not right,maybe you should post your .c
file
Exception in thread "main" java.lang.UnsatisfiedLinkError: dpapi.DPAPI.CryptProtectData(Ljava/lang/String;[BZ)[B
at dpapi.DPAPI.CryptProtectData(Native Method)
at dpapi.DataProtector.protect(DataProtector.java:60)
at dpapi.Main.main(Main.java:36)
code
public class Main {
static {
System.load(new File("jdpapi-native-1.0.dll").getAbsolutePath());
}
public static void main(String[] args) {
DataProtector p = new DataProtector();
String x ="abc";
String y = p.protect(x).toString();
}
}
I know the library is loaded but i have no idea why i am still getting the execption.
link:http://jdpapi.sourceforge.net/
check your Java CPU architecture (32bit or 64bit) equals you dll.
32-bit windows process can't load 64-bit dll and via versa...
I have PCKS11 dll and so files for my token.and i want to use [jacknji11][1] library.
my problem is how can i import the dll OR so files to my project.
i use System.loadLibrary("parskey11"); to import my library but i get below Error :
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.pkcs11.jacknji11.jna.JNANative.C_InitToken(Lcom/sun/jna/NativeLong;[BLcom/sun/jna/NativeLong;[B)I
at org.pkcs11.jacknji11.jna.JNANative.C_InitToken(Native Method)
at org.pkcs11.jacknji11.jna.JNA.C_InitToken(JNA.java:119)
at org.pkcs11.jacknji11.C.InitToken(C.java:235)
at org.pkcs11.jacknji11.CE.InitToken(CE.java:272)
at com.mmdt.Main.main(Main.java:13)
i just copy my libparskey11.so file to jni folder in project and use below code :
private static final long INITSLOT = 0;
public static void main(String[] args) {
CE.InitToken(INITSLOT, "pincode".getBytes(), "TEST".getBytes());
long session = CE.OpenSession(INITSLOT, CKS.RW_PUBLIC_SESSION, null,
null);
CE.Login(session, CKU.SO, "pincode".getBytes());
CE.InitPIN(session, "pincode2".getBytes());
CE.Logout(session);
CE.Login(session, CKU.USER, "pincode".getBytes());
byte[] somenewpin = "somenewpin".getBytes();
CE.SetPIN(session, "pincode2".getBytes(), somenewpin);
CE.SetPIN(session, somenewpin, "pincode2".getBytes());
}