DLL for a dynamic Java program?

I am currently working on a Java project where I have a dataset that I want to output in several custom formats. I have a class for each format that takes raw data and converts it accordingly. However, for starters, I use only two or three of these formats, but I want more formats to be added later without the need for a massive rebuild of the application.

My idea was to create a DLL for each of the format classes, and my application passes the data that will be converted to each of them. That way, I can later create a DLL and access my main application. (I would love to listen to any alternative ways to do this, since someone who did this in C ++ / C #, before that felt like a logical solution, but might not be applicable to Java)

My problem is that I absolutely don't know how to do this - in C ++ / C # I could write this in a few lines of code, but I'm not sure how this works with Java. At the risk of asking a terribly vague question, how can I do this?

Answers are welcome and cookies and tea will be offered. :)

Thanks in advance, M

Edit: Sorry, just add: I also don't know how to create a DLL, which should be in Java for this project, to read in the first place. Thank you. :)

+3
source share
6 answers

Instead of using the DLL as such, it seems that the desired architecture is of some kind.

One of the reasons I would not recommend using a DLL if necessary is that linking Java code with native code will require the use of the Java Native Interface (JNI), which is likely to require more effort than a pure Java solution.

One relatively easy way to do this is to use the reflection capabilities of Java.

From the above information, I would probably go along the following lines:

, , .


1:

, :

public interface Outputter {
  public void write(Data d);
}

2.

.

public class TextOutputter {
  public void write(Data d) {
    // ... output data to text
  }
}

, , class TextOutputter.class.

3.

TextOutputter.class classpath. , JVM , , class.

, , .

4.

, , - :

// Note: We load the class by specifying the fully-qualified class name!
Class<?> clazz = Class.forName("TextOutputter");

// Then, we instantiate the class.
// Note that the following method will call the no-argument constructor.
Outputter outputter = clazz.newInstance();      

// Now, we can give data to the TextOutputter object that we loaded dynamically.
outputter.write(...);

Class.forName TextOutputter ClassLoader. class, .

Class.newInstance. , , Constructor , .

, , Outputter, write TextOutputter.

, (, String, FQCN java.lang.String) - , .


, class .

( , , , , , , .)

+5

. POJO Java, . JNI - . , DLL . "" , .

+2

( DLL), java, Java Native Interface (JNI).

System.loadLibrary(String libName) ( ) System.load(String filename) ( ) (DLL) java.

+1

DLL System.loadLibrary(). Java- .

, OSGi , ( )

karaf iPOJO, .

+1

, JNI. , dll - , DLL.

Java, jar dll.

  • (, "" )
  • - , , jars
  • ,
  • .

.

+1
+1

Source: https://habr.com/ru/post/1783667/


All Articles