how can i call a java class in ruby - java

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.

Related

Calling methods of existing JRuby objects from Java

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

call method from DLL in java7

From what I understood dll are not standardized. thus one cannot just call something in a dll.
However I found this :http://johannburkard.de/software/nativecall/
This library allow you to call any method from a dll in java, so it seems that you can call any method in a dll.
but it was done for 32 bit system, thus I cannot use it.
I have this dll, autohotkey.dll, I know there is the method "ahkExec" inside which take a String as parameter.
Is this really not possible to run it from java without using some kind of c++ magic?
Thanks.
ps : here is how it is done with nativeCall : https://gist.github.com/brigand/1526712
You've been able to call DLLs in Java since version 1.0 using Java Native Interface (JNI).
There is no magic in invoking external methods but you have to follow some rules based on what JNI provides .
If you need to use one function from library you could write a specific Wrapper-class like in this tutorial
For more tricky things better to work with SWIG

using python class methods inside java

I have three different classes written in python. They contain several methods which I want to use them inside my java program (creating objects from these python classes and use these objects to call the methods). Actually my level in java might be intermediate. However, i am absolutely new in python. I have read that it is possible to use Python class methods inside java but there is no clear way that is well explained. So, please help me with a script or a tutorial or any advice that can help understanding using python classes methods in java.
Jython is the way to go. If you have a couple of hours I would recommend you go over the tutorial for it.
If you are curious to see how jython can be used inside java skip to:
http://wiki.python.org/jython/LearningJython#integrating-jython-python-into-java

Compile another java file from java file

Just out of curiosity. Can we compile & run a java file from another java program?
If so, can you send a reference to that knowledge source?
Take a look at Java Compiler Api and this little example.
If you have the java source code already in a file, then you can just call the java compiler. The java compiler is built-in to the JVM libraries as of version 1.6.
The interface is documented here.
Didn't read it thoroughly, but maybe this helps.
If you're using Java 6, the best way to do this is through the javax.tools.JavaCompiler interface.
If you're using an older version of Java, you must call javac directly using Runtime.exec(), then load the class data by subclassing ClassLoader and overriding findClass.
Yes, you can, but you need java compiler and not only java runtime. First you generate your source, save it and then use Dynamic class loading(tutorial http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html)
You can use javac (JDK is needed):
http://www.javaworld.com/javatips/jw-javatip131.html
You can do it by calling the cmd prompt or use the Main class from the Java Code.
I don't know remember well how is it, but I did it a long time ago.

How should I call a Perl Script in Java?

I read Runtime.getRuntime().exec("perl script.pl") is an option, but is this the best way to do it?
I'll need an answer from that script, so I'll have to read the script's return in some cases, although I might read it from a text file on other cases.
Anyway, is exec() a good way of calling a Perl Script from Java? I should note, I'm working on a Java Web Application, so security is an issue here.
You can use Runtime.getRuntime().exec() or use the Process API.
The Process API allows you to get the output of the script, so you can have both communicate.
exitValue() and getInputStream() seems to be what you need.
This outlines how to do it fairly elegantly, though it may be more effort than it's worth:
http://search.cpan.org/~nwclark/perl-5.8.9/jpl/docs/Tutorial.pod
Overview:
Well-supported by JPL, but it is a complicated process:
The JPL preprocessor parses the .jpl file and generates C code wrappers for Perl methods. It also generates Java and Perl source files.
The C compiler compiles the wrapper and links it to the libPerlInterpreter.so shared library, producing a shared library for the wrapper.
The Java compiler compiles the Java source file, which uses native methods to load the wrapper.
The wrapper connects the Java code to the Perl code in the Perl source file.
Fortunately, a generic Makefile.PL simplifies the process. This is a Perl script that generates a Makefile for you.
exec() is likely the best option and you can catch it's return value with exitValue(). You might also be interested in Inline::Java.
-John
keep in mind, whatever file the Perl script create, it is created in the Java working folder. just refer to that file as './myPerlCreatedFile.ext'

Categories