Jni4net with custom Java Classes or Jar - java

I saw a lot of examples on how to use primitive java classes in .NET, but how I can use my own Jar in .NET? Until now I could do this by reflection, but because I have a method with parameters File[] and String[] and jni4net doesn't have a definition for class of this (i.e. File[].class), I cannot invoke them.
I couldn't find a step by step tutorial on how I can do this. I tried with generateProxies.cmd, but I still cannot make it work.
Thanks a lot

Related

JNI_OnLoad get Java caller package+class name

Is there a way to get Java package and classname from which native library was initialized from JNI_OnLoad? I want to reuse my native library in multiple Java projects and don't know in advance classname and package from where LoadLibrary("mynativelibrary") is called. Then I could use JNI RegisterNatives with dynamic classname.
I've come across the same problem recently. I ended up reusing the class (i.e. copying the code) which loads the native library and handles all callbacks into other projects keeping the original package name. If you make that class generic and use an interface for callbacks then it shouldn't have any dependency on the project and so can be used anywhere. Works well for me.

How to read a file using javax.lang.model?

I see lot of references to javax.lang.model as being the api for parsing java files and it seems it'll suffice for what I want to do. But I can't find any examples of using the classes in it starting with opening a .java file. The only vague reference I have found are about writing a compiler plugin and invoking your code using javac.
Is that the only way to use this api? Isn't it possible to simply open a file and get the instances of classes from javax.lang.model.element with your own main()? Would anyone be able to point me to working examples of this nature?
The javax.lang.model, javax.lang.model.element, javax.lang.model.element.type, and javax.model.element.util packages are intended for use in annotation processors (javax.annotation.processing.Processor), which are the compiler plug-ins that you mentioned. There is a compiler API that you can use to compile and analyze classes at run-time. You can use javax.tools.ToolProvider.getSystemJavaCompiler() which returns a javax.tools.JavaCompiler class. It has an interface to compile .java files and to run annotation processors against them. For more information check out JavaCompiler#getTask

How to create Java classes Dynamically by Java Reflection?

Reflection is used to load java class classes and manipulate them on the fly. But I have across a weird question that is asking me how to create Java classes on the fly by Reflection.I mean the classes is not compiled or have source code till we want them created. Is it really possible? Any examples?
You can take a look look at Bean Shell's eval method It lets you execute any Java code on the fly without the need to compile the code into bytecode. You can pass a string containing all the Java code for your class to it's eval method and you'll get back an instance of the dynamically created class. Let me know if you're interested in it and want me to give you an example.
Seems to me you don't need reflection, but just need to call the JavaCompiler directly from your code: JavaCompiler.
You can try ASM
ASM
or Byte code engineering library
Byte code engineering library
for manipulating, creating classes at run time
In .NET we have Reflection.Emit(C#) which can do that
Reflection.Emit
Not sure whether there is a direct java equivalent.
You can see another similar question on SO here Java equivalent of reflection.emit
Maybe Apache DynaBeans will do. You can find some tutorials on creating and manipulating them eg. here: http://www.javaranch.com/journal/2003/07/TouringTheCommonsPart1.html

How to import DLL into the JAVA project in Eclipse?

There is DLL file called as myAPI.dll. It contains classes that I'd like to use in my JAVA code. How to import this DLL file into my Java project in Eclipse? It should be possible to run the code based on something like this:
import myAPI;
public class MyClass {
//...
}
}
I do not know which language is used for your code snippet but it is definitely not java. Java does not have keywords like using and namespace.
Generally to call native code from java you have to use good old JNI or newer JNA.
Please follow the following links to get started with these technologies.
http://java.sun.com/docs/books/jni/html/start.html
http://www.javaworld.com/community/node/1767
The code snippet you have shown is C#, not Java. Now namespace lets you group logically related things. For e.g. all order processing related classes can be put under single namespace. It is also used as a mechanism to avoid/resolve name conflicts. It also defines the visibility scope of your class. Read this lesson for more details.

How can I dynamically create a new .java file from a Java program?

I want to create a one .java from the Java program. When I run the program, automatically one Java file will created in my project, and also create some run time (dynamic) variable in that file. How can I do this?
I know for this I have to use a Reflection API like Class and Method, but what are the methods in Class and Method to do this?
You cannot create new classes or methods using the reflection APIs. They are not designed for this. (The Class and Method APIs are for performing operations on object instances in a dynamic fashion.)
If you want to create new code on the fly, there are two basic approaches to doing this:
Generate Java source code, write it to a file, use the Java compiler to compile it to a bytecode file, and then load the bytecodes. (There are standard APIs for running the Java compiler within the JVM of a running application.)
Use BCEL or equivalent to construct a bytecode file from scratch, and then load the bytecodes.
Both approaches are tricky and computationally expensive. The BCEL approach is particularly tricky because you need to understand a lot about the JVM to do the job.
Apparently you want to create a new class at Runtime and use it. You can sure create a .javafile, compile it and load it from a custom class loader but that's probably not the best/easiest thing to do. Here are a bunch of solutions:
First of all if you want to extend an interface, you can use the Proxy from the Java Reflection API.
It you want to extend a class rather than implements an interface or create a class out of the blue you need to use a library to create bytecode. You can find a bunch of them on http://www.java-opensource.com/open-source/bytecode-libraries.html. Among these libraries I like javassist mainly because it is the only library to my knowledge letting you enter Java code directly rather than bytecode.
A last solution should be to use a framework like Groovy or BSH to interpret pseudo-java code.
No, you can't generate new .java files using Reflection. You could perhaps create a new class, and use this class, in runtime, but you can't write that class out to file in the form of a .java source file.
Have a look at the JustAdd framework for instance. This framework solves this type of problems IIRC.
Java is a strongly typed language( As opposed to a weakly typed language). Simply put you need to have a Class (prototype) to create a instance of object. What you are trying to do is not natural in java (or any strongly typed language).
If you have to have this functionality in java, you need to use groovy. Groovy is a dynamic language that can run in Java JVM. You need to check Expandos in groovy.(ofcourse it still will not create a .java file).

Categories

Resources