Let's say I'm writing an app in IronPython and I'd like to use some classes stored in a Java jar (which I don't control and I'd rather not wrap in a DLL).
I downloaded IKVM and tested that command line tools work fine.
Let's say the class I'd like to access is like this:
package some.thing;
class Hello {
public static void myMethod(String arg){
System.out.println("you got me!");
}
}
Which DLLs should I import from IronPython in order to be able to then call Hello.myMethod('a')?
Is this possible at all with IKVM? If not, is there any other way you can see to make this work?
Related
I'm making a game engine using LWJGL. The developer using it has to be able to use scripts. I decided to just make them use Java because writing an API in another language wasn't something I'm going to have the time nor experience to do. Anyways, I would have used x.main(); to run it, but The developer tells what the script is named, and that is stored in a variable. I just thought I could run a command to do that, using a method like exec() in python or eval() in JavaScript. I couldn't find a straightforward library that has this execution functionality.
To summarize this paragraph, I need a Java Library that can use a method like JavaScript's eval() or python's exec()
I dont know If I understood the problem, but I have focused on part of having "script name stored as variable" which sounds to me like a method name. You can invoke method by its name using reflections
public class MCAlu {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
String scriptName = "sayHello";
Method scriptMethod = MyScript.class.getMethod(scriptName);
scriptMethod.invoke(null, null);
}
}
class MyScript {
public static void sayHello() {
System.out.println("Hi there!");
}
}
Since class has to be known and on the classpath (unless you will load it in the runtime), class name can be as well provided as string resulting in
String scriptClass="MyScript";
String scriptName = "sayHello";
Method scriptMethod = Class.forName(scriptClass).getMethod(scriptName);
scriptMethod.invoke(null, null);
One (quite popular) tool that can help you run Java sources as scripts is JBang. Java programs have to be compiled (to Java class files) to be able to run by a JVM. So, basically, JBang hides this compilation step and invokes the JVM with our compiled class.
I am having a .dll file which has a wrapper in C,C++, .Net and python, but not in Java. I am successful in loading the .dll file using following code in my Java
public class
public static void main(String[] {
// print when the program starts
System.out.println("Program starting...");
System.out.println("Loading Library...");
Runtime.getRuntime().loadLibrary("HelloJava");
System.out.println("Library Loaded.");
}
}
which gives the following output:
Now my question is that if this file is loaded, How do I access it's functions to use in my Java workspace?
Since it is the C-DLL, so How should I fetch the module values from this .dll.
Note:
I have made a folder named dll under my Java project path from where I loaded the library in the above code.
I browsed for the concept for JNA and JNI but lacked the understanding concept, that's why posted the question.
Thanks in advance.
I think you need to know the methods in your DLL first. I really don't know how to list the methods using JNA or JNI, but you have to know the method's signature before starting, maybe from a documentation, because you normally can find the DLL's documentation on the web, or you even can use a Reflector (like red-gate) to find out your methods.
Then:
Download the JNA .jar file and add it to your build in your Java project.
Put the .dll in the root directory of your Java project.
Create an Interface that contains the functions from the dll that you want to use.
For example, lets say your HelloWorld DLL has a String hello(String hey) method in C++, then in your Java project you will have to do something like:
import com.sun.jna.Library;
import com.sun.jna.Native;
public class Main {
public interface Ihello extends Library {
public String hello(String hey);
}
public static void main(String args[]) {
//"hello-world is the name my DLL, for example.
Hello h = Native.load("hello-world", Hello.class);
System.out.println(h.hello(" John! ");
}
}
Here is a good example, regards.
I'm looking on guidance on how I can essentially create an 'empty shell' jar with maven. The idea is I have a java project, and I want to export the my.project.api classes (with package) into its own jar without saving the methods / constructors actual code inside.
For example, lets say I have the following:
public class Test {
public void doSomething(String message) {
System.out.println(message);
}
}
I want to export a separate jar which would keep its package declaration, and export as:
public class Test {
public void doSomething(String message) {}
}
The reasoning for this is the project itself is exclusive, but I want to allow other developers to make their own integrations without the need of the physical product / project. This way by them hooking into say my.project.api.Test, they'd be able to see the methods and do as they wish.
Hopefully this clarifies enough, it would export as a separate jar maybe as 'MyProject-API.jar' or something.
Thanks!
This very much looks like a use case for interfaces.
I'm newbie to both Mercurial and Python and I was trying to implement an extension and was wondering if it's possible to fire-up or execute a Java method if I defined an hook within mercurial.ini file.
Let's say I had a Java method:
Java method
public class Hello {
public void doSomething(String str) {
System.out.println("Hello World");
}
}
Mercurial.ini
I know we can call a python function like this:
precommit = python: d:\workspace\Hello.py:doSomething
but can we do something like similar by calling Java method:
precommit = java: d:\workspace\Hello.java:doSomething
I tried doing above but didn't work for me!
Many thanks!
It's possible but not that simple. Mercurial doesn't know how to start Java or how to call methods in it.
The solution is to create an executable JAR and then use this code:
precommit = java -jar YourJarFile.jar ...arguments...
as a hook (without a prefix, the hook is expected to be a system command).
I have a library which is written in C++ (actually a Firefox plugin, xyz.dll) and I need to access its methods from Java.
public class AccessLibrary {
public interface Kernel32 extends Library {
public void showVersion();
}
public static void main(String[] args) {
Kernel32 lib = (Kernel32) Native.loadLibrary("xyz.dll", Kernel32.class);
lib.showVersion();
}
}
While executing got the following error:
java -jar dist/accessLibrary.jar
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'showVersion': The specified procedure could not be found.
In the native library source code, the method is defined like this
void CPlugin::showVersion() {
/* ... */
}
I am very new to Java. May be I am missing something basic. Looked into similar questions but none of them solves my problem.
Forgot to mention I am using Windows 7 64bit and Java 7.
First, you cannot export a class method and load it into java. The name will get mangled, and java wouldn't know how to call it properly. What you need to do is break it out into a separate function on its own.
After that:
As already pointed out, make sure you export the function. You can export using one of two ways. The first is what is mentioned, which is to use __declspec( dllexport ). The second is to put it into the def file.
Additionally, make sure you mark it as extern "C" otherwise the name will get mangled. All the details are here: Exporting functions from a DLL with dllexport
So the the signature should be something like this:
extern "C" __declspec(dllexport) void showVersion () {
}
Finally, the depends tool can be downloaded here: http://www.dependencywalker.com/
I think your native library needs to provide a C-style interface, for example
__declspec( dllexport ) void showVersion() {
/* ... */
}
Ideally, take a look at your DLL with depends.exe (which is available through the Windows SDK), there you'll see if your DLL provides the correct function exports.