I'm trying to provide a JRuby callback to my Java class, so I can get notified when an event happens in the Java part of my program (a OrientDB Java Hook).
The JRuby wiki offers examples demonstrating how to load JRuby files and run them in Java, but in my case, the Ruby codes are already in the same JVM, and I just need to send signals to existing Ruby objects.
Assume I have the ruby class loaded in memory:
class A
def self.b
puts 'ruby called'
end
end
How should I call A.b from my Java method?
Turns out I can just use JRuby in this case.
For the OrientDB embed example, instead of a Java class, I can pass a ruby class
class H < com.orientechnologies.orient.core.hook.ORecordHookAbstract
def onRecordAfterCreate(r)
puts r
end
end
to the API and let Java code call Ruby.
Relevant gist
Related
I saw Calling a groovy script from a java function using javax.script I guess with the runWithGroovyClassLoader can get a java class, java object and call its method with arguments. Though in the example its a no args example.
I a solution to be able to call type script. So my users enter type script in the front end. In the back end we call the script from within a Java function, passing it some state (arguments) of current txn from Java and then the type script returns a map / object back to calling java function
You'd be looking for a TypeScript engine that was compatible with the Java Scripting API. You probably won't find one, but you might.
However: The JDK ships with a JavaScript engine (Nashorn). If you transpile your TypeScript to JavaScript (via the TypeScript compiler, tsc), you can then run the resulting JavaScript via javax.script. You'll need to ensure that tsc is targeting "ES5", I don't think Nashorn supports ES2015+ yet.
I have some wscript / vbscript based code that calls a certain COM API. This code needs to be run periodically with lots of other Java based code that we run in order to gather metrics.
I know that I could take this code and translate it to something that uses Com4J, JNA or alike to mimic the behaviour in Java. But the vbscript code is "certified" by the vendor of the application we measure and I want to avoid debates that when I translated the code I changed the validity of the measurements (we had a comparable debate before)
A second alternative would be to simply call wscript.exe as a subprocess, have it write the results to stdout and parse that. But I would rather avoid that because then I would have to check for stalled subprocesses etc.
Is there a different way to call WSH from Java ? Perhaps by calling the Windows Scripting Host as a COM Object and passing it a pointer to the sourcecode or the source in a String ?
I have a DLL file which has a class defined as 'Handler' which in turn contains method call getProperty(string,string).
How can I access such method from a JAVA program??
** I see a lot of examples on net which allows invocation of global function defined inside the DLL, but don't see any examples where we can invoke an method on a native object.
As mentioned in comments JNI is something you are looking for, but you will not be able to use "native" class/objects on Java side.
You'll need to add "native C" bridge layer with pure C API, which will wrap your C++ interface and translate calls to DLL, because only such API can be used on Java side via JNI.
I'm currently coding a game in Java that is growing bigger over time.
So now I'm at a point where more dynamic implementations of code would become quite handy.
I decided to take a look into Jython and got it working with some simple scripts already.
(Btw I'm using the newest standalone Jython if it matters somehow).
Now my Question is: Can I execute methods that are in my Java project in a Jython script that is executed in the mentioned Java project?
Here is an example of pseudo-code for better understanding what I want to ask:
Let's say I have a Script that looks like the following:
def main():
killPlayer()
main()
And a Java class that contains the method "killPlayer()":
public void killPlayer() {
player.setAlive(false);
}
While the Jython script is executed as following:
PythonInterpreter pyInterp = new PythonInterpreter();
pyInterp("script.py");
Is anything of that kind possible?
Thanks in advance and sorry for my poor english ^^
You can import Java classes in Jython like so:
from javax.swing import JFrame
f = JFrame('Hello, World!', defaultCloseOperation=JFrame.EXIT_ON_CLOSE, size=(300, 300), locationRelativeTo=None)
f.setVisible(True)
(source)
So my suggestion is to use the Java code as a library and implement the "setup everything" in Python. That way, you won't need to figure out a way to look up instances of Java objects from the script.
If you need to look up instances, I suggest to create a static global variable somewhere which gives you access to a class that exposes important game instances. In the Java code, you can then register the instances with a name in a Map for the Jython scripts.
How can i call a java class in ruby?
If you use JRuby, you can
require 'java'
and then instantiate a Java class using eg
object = Java::package.package.ClassName.new
and then call methods using
object.method(parameter)
for more information, see Scripting Java libraries with JRuby
try this Java/Ruby Bridge:
Link
The Bridge itselfs enables you to load java classes into your ruby Code.
Can you wrap, or call, the Java class with another Java class with a main() method reading stdin or command-line arguments ? You can then spawn that as an executable from Ruby, write to stdin and read from stdout.
That may be the simplest answer, bearing in mind it's not the fastest mechanism, or in some cases the most practical. For some scenarios, however, it may be the most pragmatic.