I need to reference a .Net dll in java. I have used jni4net libraries for the same. I have followed the steps mentioned in the video below :
https://www.youtube.com/watch?time_continue=351&v=8OoSK_RWUe4
I have followed all the steps required to reference jni4net libraries but i get the following runtime Exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: orionforpython.DynamicOrion.__ctorDynamicOrion0(Lnet/sf/jni4net/inj/IClrProxy;)V
at orionforpython.DynamicOrion.__ctorDynamicOrion0(Native Method)
at orionforpython.DynamicOrion.<init>(DynamicOrion.java:25)
at com.orion.OrionForJava.main(OrionForJava.java:16)
After following all the steps, This is my code:
package com.orion;
import net.sf.jni4net.Bridge;
import orionforpython.*;
import java.io.*;
class OrionForJava {
public static void main(String[] args) throws IOException {
Bridge.setVerbose(true);
Bridge.init();
File proxyAssemblyFile=new File("OrionForPython.dll");
Bridge.LoadAndRegisterAssemblyFrom(proxyAssemblyFile);
DynamicOrion orion=new DynamicOrion();
String res=orion.ReqLogin("user", "pwd", "");
System.out.print(res);
}}
I have tried executing the same using NetBeans 8.1 IDE but with no success. I am using jni4net-0.8.8.0 version and Eclipse IDE for Java Developers
Version: Oxygen.3 Release (4.7.3)
Any assistance would be helpful!
I used jni4net library to call c# dlls from java and it is working fine. I used a lightly different approach to initialize jni4net.
try {
Bridge.setVerbose(true);
Bridge.init(new File("Full path to jni4net.n.w64.v40-0.8.8.0.dll"));
// where dlls to load is jni4net.n.w64.v40-0.8.8.0.dll,jni4net.n-0.8.8.0.dll,MyOriginalNETDll.dll,MyOriginalNETDll.j4n.dll (after proxygen processing)
for (String str : dllsToLoad) {
File dll = new File(rutaDlls + str);
Bridge.LoadAndRegisterAssemblyFrom(dll);
}
} catch (IOException e) {
LOG.error("Error jniBrige.", e);
}
I needed to use full path c:... to the dll to make it work. I also had to take care about .net framework version used to create assembly (need to use 4.0 in my case and java version 8)
Hopes this helps
We use JCOBridge which can be used in .NET Core (>= 3.1), .NET 5/6 and .NET Framework (>= 4.6.1). Referencing the DLL you need to call you will have full access to it and you can use it in your projects.
Consider the following C# snippet class available in a generic TestBridge.dll:
using System;
namespace TestBridge
{
public class MyClass
{
/// <summary>The method <c>HelloWorld</c> return the "Hello World!!" string</summary>
public String HelloWorld()
{
return "Hello World from C#!!";
}
/// <summary>The method <c>Add</c> return the sum of two double</summary>
public double Add(double a, double b)
{
return a + b;
}
/// <summary>The method <c>Add</c> return the sin of a double</summary>
public double Sin(double a)
{
return Math.Sin(a);
}
}
}
The methods of the previous class can be invoked from the following java code snippet:
import java.io.IOException;
import org.mases.jcobridge.*;
public class CSharpClassUse {
public static void main(String[] args) {
try {
try {
try {
JCOBridge.Initialize();
} catch (JCException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
return;
}
//declare and create JCOBridge instance
JCOBridge bridge;
bridge = JCOBridge.CreateNew();
// adds the path where external assemblies can be found
bridge.AddPath("Path where is TestBridge.dll assembly");
// add REFERENCES to the .dll file you want to invoke
bridge.AddReference("TestBridge.dll");
// INSTANTIATE the .NET Object: JCObject is a meta object
JCObject theNetClassInstance = (JCObject) bridge.NewObject("TestBridge.MyClass");
double a = 2;
double b = 3;
double c = Math.PI/2;
//Invoke the C# class methods
String hello = (String) theNetClassInstance.Invoke("HelloWorld");
double result = (double) theNetClassInstance.Invoke("Add", a, b);
double sin = (double) theNetClassInstance.Invoke("Sin", c);
System.out.println(String.format("%s %.0f + %.0f = %.0f and sin(%.8f) = %.8f", hello, a, b, result, c, sin));
} catch (JCException jce) {
jce.printStackTrace();
System.out.println("Exiting");
return;
}
}
}
The previous java code produces the following output:
Hello World from C#!! 2 + 3 = 5 and sin(3,14159265) = 1,00000000
The previous example shown how use a C# class available in a DLL. If you need to invoke/integrate .NET graphic, that in generic sense are C# DLL too, JCOBridge also manages GUI integration (WPF/WinForms/AWT/Swing): look at these Examples
Hope it was useful and clear.
Related
This is my code and i have added the .dll to the place where Java_Home is. And i have this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: proiectP.JavatoC.getval(I)I at proiectP.JavatoC.getval(Native
Method) at proiectP.JavatoC.main(JavatoC.java:19)
public class JavatoC {
public native int getval(int b);
static {
System.loadLibrary("main");
}
public static void main(String[] args) {
try {
int a;
int b=3;
a= new JavatoC().getval(b);
System.out.println(a);
} catch(Exception e) {
System.out.println(e);
}
}
}
I also tried to write a= new proiectP.JavatoC().getval(b) as proiectP is the package. It doesn't work.
It seems library link not done right. There is a problem in the System.loadLibrary. Because it can't show the required method.
These are requirements for using a native code in Java, And I don't know which has not been observed :
First, make sure that the native file is correct, the getval method must be in that dll, exactly with the same specifications of name, input and output.
Second, it is exactly compiled for this use.
The last is in the right direction with the right name.
This question already has answers here:
How do I create a file and write to it?
(35 answers)
Closed 3 years ago.
I want to create a java program that generates another java class in the same project. For example in the class Dragon.java, i want to write java code that creates another java class called fire.java. I do not want to use any GUI from eclipse, just pure code that generates another class from the execution of written programming in java.
I have tried making objects of a non existent class in hopes of the program automatically producing a class with that name.
Again, it doesn't have to be just a java class, is there a way to make other forms of files also? for example fol.flow, or of different names.
Creating a new Java file is easy. You can use any FileWriter technique. But what need to be taken care of is that new Java file is valid java file and can be compiled to class file.
This link has working example of doing the same.
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
public class MakeTodayClass {
Date today = new Date();
String todayMillis = Long.toString(today.getTime());
String todayClass = "z_" + todayMillis;
String todaySource = todayClass + ".java";
public static void main (String args[]){
MakeTodayClass mtc = new MakeTodayClass();
mtc.createIt();
if (mtc.compileIt()) {
System.out.println("Running " + mtc.todayClass + ":\n\n");
mtc.runIt();
}
else
System.out.println(mtc.todaySource + " is bad.");
}
public void createIt() {
try {
FileWriter aWriter = new FileWriter(todaySource, true);
aWriter.write("public class "+ todayClass + "{");
aWriter.write(" public void doit() {");
aWriter.write(" System.out.println(\""+todayMillis+"\");");
aWriter.write(" }}\n");
aWriter.flush();
aWriter.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public boolean compileIt() {
String [] source = { new String(todaySource)};
ByteArrayOutputStream baos= new ByteArrayOutputStream();
new sun.tools.javac.Main(baos,source[0]).compile(source);
// if using JDK >= 1.3 then use
// public static int com.sun.tools.javac.Main.compile(source);
return (baos.toString().indexOf("error")==-1);
}
public void runIt() {
try {
Class params[] = {};
Object paramsObj[] = {};
Class thisClass = Class.forName(todayClass);
Object iClass = thisClass.newInstance();
Method thisMethod = thisClass.getDeclaredMethod("doit", params);
thisMethod.invoke(iClass, paramsObj);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
At first I thought you wanted code generation, but you simply want to write to files or create them?
The simplest code to create file and write to it:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class Testing {
public static void main(String[] args) throws IOException {
Files.writeString(Paths.get("D://output.txt"), "some text to write", StandardOpenOption.CREATE);
}
}
It uses only java standard classes, you don't need any libraries or anything external. Just make sure to write to the valid path, where you have access.
If you want to generate files with java code, you can just do it with the method above, but creating the String with code content is really hard, there are libraries for it and they are not easy to use for beginners. For example javapoet. I personally used javaparser, it has a lot of other possibilities besides generating code.
I am new to accessing DLLs from Java using JNA. I need to access methods from a class within a DLL(written in .net). Form this sample DLL below, I am trying to get AuditID and Server ID. I am ending with the following error while I am running my code. Any guidance really appreciated.
/// Error ///
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'GetEnrollcontext': The specified procedure could not be found.
//DLL File Code//
SampleDLL.ProfileEnroll enrollcontext = new SampleDLL.ProfileEnroll();
enrollcontext.Url =” url”;
enrollcontext.AuditIdType = SampleDLL.ProfileId;
enrollcontext.AuditId = “22222222 “;
enrollcontext.ServerId = “server1”;
/// Java Code ///
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Structure;
import dllExtract.DLLExtractTest.SampleDLL.Enrollcontext;
public class SampleDLLExtract {
public interface SampleDLL extends Library {
SampleDLL INSTANCE = (SampleDLL) Native.loadLibrary("SampleDLL",
SampleDLL.class);
public static class Enrollcontext extends Structure {
public String auditId;
public String serverId;
}
void GetEnrollcontext(Enrollcontext ec); // void ();
}
public static void main(String[] args) {
SampleDLL sdll = SampleDLL.INSTANCE;
SampleDLL.Enrollcontext enrollContext = new SampleDLL.Enrollcontext();
sdll.GetEnrollcontext(enrollContext);
System.out.println(sdll.toString(sdll.GetEnrollcontext(enrollContext)));
}
}
in fact there is a solution for you to use C#, VB.NET or F# code via JNA in Java (and nothing else)! and it is also very easy to use:
https://www.nuget.org/packages/UnmanagedExports
with this package all you need to do is, add [RGiesecke.DllExport.DllExport] to your methods like that:
C# .dll Project:
[RGiesecke.DllExport.DllExport]
public static String yourFunction(String yourParameter)
{
return "CSharp String";
}
Java Project:
public interface jna extends Library {
jna INSTANCE = (jna) Native.loadLibrary("yourCSharpProject.dll", jna.class);
public String yourFunction(String yourParameter);
}
use it in the code:
System.out.println(jna.INSTANCE.yourFunction("nothingImportant"));
Viola!
As already mentioned it works very easy, but this solution has some limitations:
only available for simple datatypes as parameter & return values
no MethodOverloading available. yourFunction(String yourParameter) and yourFunction(String yourParameter, String yourSecondParameter) does not work! you have to name them differently
Use arrays as parameter or return values. (JNA offers StringArray, but I am not able to use them in C#) (maybe there is a solution, but I couldn't come up with one so far!)
if you export a method you can't call it internally in your C# code (simple to bypass that by the following:
.
[RGiesecke.DllExport.DllExport]
public static Boolean externalAvailable(String yourParameter)
{
return yourInternalFunction(yourParameter);
}
With C# it works great, with VB.NET and F# I have no experience.
hope this helps!
I have an agent that is set to run every day at 8:00
I want to write a java code (in a shared library) and call that library from the agent with parameters.
For Example:
Agent code:
// ....
checkAndSendMail(email_1);
checkAndSendMail(email_2);
// ....
java library code:
public class Check{
public void checkAndSendMail(String email_param){
// ...
mail.send(email_param);
// ...
}
}
Can I call a java shared script library from a Java agent in Xpages?
if yes, then how to call?
The JVM in XPages and Domino Java Agents is separate so you can't share java code between them.
You can create java code if you go to script libraries section in the designer
not the Java/Jar section that is for XPages. And create a new Java Library that can be included inside a Java agent.
You can do this, but this is only possible with a lot of "overhead". Assuming you want to load a Java class in an Agent you could do the following:
Get the design note containing your class (f.e. with a special design view or the Java NAPI)
Export the note with DXL
Extract the content all "$ClassData" fields
Base64 decode the content
Skip the first 42 bytes , and load the resulting byte array with your own class loader (override the findClass method which does a defineClass call)
Now you can instantiate the class in your agent and access it via reflection
As you can see, it is possible, but for a higher effort than just "doubling" the libraries in the DDE.
EDIT:
Here is an example class loader for an agent. The Base64 encoded DXL is already added.
The agent instantiates the class ch.hasselba.demo.LoadedClass and calls the method printTime():
package ch.hasselba.demo;
public class LoadedClass {
public void printTime(){
System.out.println("Time: " + System.currentTimeMillis() );
}
}
The code of the agent (uses lwpd.commons.jar)
import lotus.domino.AgentBase;
import com.ibm.commons.util.io.base64.Base64;
import java.lang.reflect.Method;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
// trucated DXL string
String dataDXL = "YQAYAAAAAACqAgAAAQAAAAAAAAAAAAAAYAC8AgAAqgKqAgAAAAAAAAAAyv66vgAAADEALwcAAgEAFWNoL2hhc3NlbGJhL3hwYWdlcy9aWgcA";
// base64 decode the string
String b64 = Base64.decode(dataDXL);
byte[] b64Bytes = b64.getBytes();
byte[] classBytes = new byte[b64Bytes.length - 42];
// skip the first 42 bytes
System.arraycopy( b64Bytes, 42, classBytes, 0, b64Bytes.length - 42);
try {
// load the class
ByteClassLoader obj = new ByteClassLoader();
Class theClass = obj.findClass("ch.hasselba.demo.LoadedClass", classBytes);
// instantiate it
Object theInstance = theClass.newInstance();
// get the method printTime via Reflection & call it
Method theMethod = theInstance.getClass().getMethod("printTime", null);
theMethod.invoke( theInstance, null);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// the class loader
public static class ByteClassLoader extends ClassLoader {
public Class findClass(String name, byte[] data) {
return defineClass(name, data, 0, data.length);
}
}
}
Mike, Fredrik is right - no sharing. Unless...
you package your shared code into a Jar and deploy that one to the jvm/lib/ext directory of your server and/or client. Your admin will not like that potentially. There was a patched version of the updatesite.ntf on OpenNTF that allowed to deploy plug-ins into the server OS. You could hack the script to deploy a jar into the ext directory. But please only with admin's consent.
:-) stw
I am using dll in java using JNA, but i am getting below error
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'GetStatus': The specified procedure could not be found.
Not getting how to resolve this issue?
Please help.
Here is java code
import com.sun.jna.Library;
import com.sun.jna.Native;
/** Simple example of native library declaration and usage. */
public class First {
public interface TEST extends Library {
public String GetStatus();
}
public static void main(String[] args) {
TEST obj = (TEST ) Native.loadLibrary("TEST ", TEST .class);
System.out.println( obj.GetStatus());
}
}
This Nugget is super easy to use and works perfectly. https://www.nuget.org/packages/UnmanagedExports
You need Visual Studio 2012 (express).
Once installed, just add [RGiesecke.DllExport.DllExport] before any static function you want to export. That's it!
Example:
C#
[RGiesecke.DllExport.DllExport]
public static int YourFunction(string data)
{
/*Your code here*/
return 1;
}
Java
Add the import at the top:
import com.sun.jna.Native;
Add the interface in your class. Its your C# function name preceded by the letter "I":
public interface IYourFunction extends com.sun.jna.Library
{
public int YourFunction(String tStr);
};
Call your DLL where you need it in your class:
IYourFunction iYourFunction = (IYourFunction )Native.loadLibrary("full or relative path to DLL withouth the .dll extention", IYourFunction.class);//call JNA
System.out.println("Returned: " + IYourFunction.YourFunction("some parameter"));
EDIT:
If the DLL is 32bit, the JDK/JRE has to be 32bit as well. Add the following check to your code:
if(!System.getProperty("os.arch").equals("x86")) {
throw new Exception(".NET DLL " + "32bits JRE/JDK is required. Currently using " + System.getProperty("os.arch") + ".\r\nTry changing your PATH environement variable.");
}