Using JRuby to directly call Android Java Methods - java

I've been recently exploring how to access Android's Libraries via JRuby in SL4A. I know it is possible to design apps with Ruboto, but I just want to right a simple script to access APIs that current SL4A doesn't offer. I can import normal jars and such but I haven't been able to get Android's API. In specific I want to access 'android.nfc'. Is there a way do accomplish this that I haven't figured out yet or is it possible to not work; SL4A does state that JRuby offers a direct API bridge though.
Thanks,
Clement

Can you include your Jruby code? Are you building your classpath properly and then doing the java_import as well?
I have a ruby file just for loading up my jars into my classpath that I require in every .rb file called java_jars.rb it contains the following:
Dir["target/dependency/\*.jar"].each { |jar| require jar }
then in the main ruby file do this:
include Java
require 'java_jars'
java_import 'com.package.name.blah.ApiClassName'
def methodName
apiObject = com.package.name.blah.ApiClassName.new
# don't forget methodName becomes method_name in JRuby
apiObject.method_name
end

Related

How to include a library in Java which in C++ with having native binding in Java (librets)?

I am stuck at importing a library which is originally written in C++, but has native binding for Java. Here is the library https://github.com/NationalAssociationOfRealtors/libRETS, and I was able to build it through the doc in doc/build, but what after that? I see some makefiles in project/build/ and I want to import this library in Java. Any help will be really appreciated as I cannot find anything in the documentation, all I know is there are some makefiles and the description claims that this library has native bindings for other languages.
Watch the output of ./configure carefully and make sure the build is configured to create the SWIG components (namely, for Java.)
Option summary:
Use ccache .................: no
Use dependency checking ....: no
Use -fPIC...................: yes
Use shared dependencies.....: yes
Compile type................: Normal
Compile examples............: no
Compile SQL compiler........: no
Compile SWIG bindings.......: no <-------- should say yes
With DotNet...............: no
With Java.................: no <--------- me too
With PERL.................: no
With PHP..................: no
With Python 2.............: no
With Python 3.............: no
With Ruby.................: no
With Node.js..............: no
Enable Maintainer Docs......: no
I tried it and a fairly recent version of SWIG was required -- more recent than were in my package manager. Without that, the SWIG bindings don't get built and there's no Java.
However, once you do get that build, it should be a fairly straightforward endeavor of calling into a jar file, as with any other Java project. Who knows, the build might even generate Javadoc for you so you have some idea of what to call.

How can I specify entry-point class and jar archive for JBCO (Java ByteCode Obfuscator)?

I can't guess how can I specify class, which is entry-point of my program (therefore shouldn't be obfuscated), and my jar archive. Please show me an command-line example, how to use JBCO when I have /home/example/myJar.jar and within it com.example.EntryPoint class and my external dependency /home/example/dependencies/dependencyJar.jar.
Also, please, does anybody know if this project is still alive and what jdk it supports?
A lot of time have passed, but recently I have passed across the java transformation frameworks and find out that JBCO now is a part of soot framework, hosted on GitHub, but it is #deprecated as for now. There is a wiki where you can get more info about how to use soot/jbco (if you still want to, on your own risk, even though JBCO is deprecated and not under active development it still from time to time accepts PRs from contributors).
As for the command line options it might be:
java -cp .:/home/example/sootclasses-trunk-jar-with-dependencies.jar soot.jbco.Main -process-dir /home/example/compiled -output-dir /home/example/obfuscated -soot-class-path .:/home/example/myJar.jar -output-format class -app -main-class com.example.EntryPoint -t:9:wjtp.jbco_cr
Soot can process your compiled code as class files (then pass it to -process-dir option) or as jar (then pass it as part of soot-class-path) - soot can process many forms of bytecode (java/scala/.. bytecode, android bytecode, jasmin, jimple). There are also options to specify what is library classes and application or argument classes more precisely, for more info please refer to soot's wiki page.

How to package in a .jar java main + jruby compiled class + ruby script?

I am building a Spark application that make use of some feature already developed in Ruby.
I made the choice to invoke the Ruby part from my Scala main by defining MyProxy class in ruby and compiling with JRuby.
I can then use MyProxy to invoke the rest of the Ruby code that stay in scripts. Foremost reason is that I was unable to compile them with JRuby, probably because they are too dynamic:
## myProxy.rb -> compiled into myProxy.class
## jrubyc --javac myProxy.rb
require 'java'
java_package 'ruby.proxy'
require_relative 'some.rb'
class MyProxy
def self.invoke_script()
... ## invoke some other ruby in script that are note compiled by jrubyc
end
end
and the Scala Main:
object myRun extends App {
val something = MyProxy.invoke_script()
...
}
At runtime the flow looke like this:
Main.class (scala) -> call myProxy.class (compiled ruby of myProxy.rb) -> call function in script.rb
It works, and I was able to make a runnable jar for the Scala and compiled ruby part. But when I am running it: java -jar myApp.jar,
it still need to access my myProxy.rb file and, of course, all other scrips.rb.
So I need a copy of all my ruby scripts in the working directory when executing this command.
Ideally I would like to include all the ruby scripts in the myApp.jar as well, and be able to deploy easily deploy on a spark cluster.
Is this possible, and how?
I have looked at warbler and rawr. However, I don't see how these tools can help me in this mixed environment (main in Java, some part compiled ruby, some part pure scripts).
Any help appreciated!
As stated in the jruby documentation, packaging ruby scripts in a jar, should be has simple as including them in the .jar (sic):
Everything in the Java classpath is considered to be a load path entry, so .rb scripts, for example, contained in JAR files, are loadable.
It wasn't working in my case, because I was using require_relative in my script, which doesn't seem to work properly in recent release of JRuby. Replacing require_relative with require made it works with scripts embedded in myApp.jar.
Moreover using require_relative 'toto' in titi.rb was triggering an error of titi.rb script not found, which was misleading since it was titi.rb which was being executed.

Running Java class with JMeter (Bean Shell)

I have written a Java Class for use in JMeter, packaged the project as a .jar file and moved that file into the lib/ext folder in the jmeter directory. I have seen documentation on how to proceed but they give contradictory answers.
The first way is to use the BeanShell Sampler to import my package and class, create an object of the class and run the methods that way. I have used this method using example classes with more simple file structures than that of class I want to run. The example classes work with the following BeanShell script.
import tools.JmeterTools;
JmeterTools jt = new JmeterTools();
jt.foo();
When I try to use this method for the class I want to run, it states that the variable declaration is an error and the Class cannot be found. I assume this is because I do not understand what to import exactly, as the file structure in my project is a little odd.
The second uses the BeanShell PreProcessor to add the jar to the class path. This method I have not been able to get to work at all, but have read many accounts of others finding success. It works as follows:
addClassPath("directory path to jar\lib\ext\foo.jar");
JMeterTest jtm = new JMeterTest();
jmt.test();
Would anyone have any knowledge of which way would work better or any ideas on how to fix the import?
The import I have been using in the BeanShell script is the following:
import client.JMeterTest;
The package line at the top of my class is the following
import com.x.foo.client;
You need to have your jar file in JMETER_HOME/lib folder.
lib/ext is for JMeter extensions/plugins etc.
Once you have placed your jar, you might have to restart JMeter.
Running external classes from Beanshell should work fine given the following preconditions met
Your test with dependencies is located in JMeter classpath.
JMeter restart is required to pick new libraries up
You need to provide full package name plus full class name (or wildcard) for import.
Either
import com.x.foo.client.JMeterTest;
or
import com.x.foo.client.*;
And finally it is recommended to use JSR223 Sampler and use "groovy" as a language. Beanshell interpreter has severe performance issues so use it for something very "light" like variable amendment, converting variable to property, etc. For generating the real load use JSR223 and groovy as it implements Compilable interface and hence you can achieve performance similar to native Java code. See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! guide for detailed explanation, benchmarking and instructions on installation of groovy scripting engine support.
For anyone who has this issue in the future. The answers given by others are correct. It wasn't working for me because I had forgotten that Maven does not package files in the test directory when a jar is made.
This link may help if anyone ever does this in the future.
Generate test-jar along with jar file in test package

Jython and python modules

I've just started using the PythonInterpreter from within my Java classes, and it works great! However, if I try to include python modules (re, HTMLParser, etc.), I'm receiving the following exception (for re):
Exception in thread "main" Traceback (innermost last):
File "", line 1, in ?
ImportError: no module named re
How could I make the classes from the jython jar "see" the modules python has available?
You embed jython and you will use some Python-Modules somewere:
if you want to set the path (sys.path) in your Java-Code :
public void init() {
interp = new PythonInterpreter(null, new PySystemState());
PySystemState sys = Py.getSystemState();
sys.path.append(new PyString(rootPath));
sys.path.append(new PyString(modulesDir));
}
Py is in org.python.core.
rootPath and modulesDir is where YOU want !
let rootPath point where you located the standard-jython-lib
Have a look at src/org/python/util/PyServlet.java in the Jython-Source-Code for example
According to the FAQ:
4.1 What parts of the Python library are supported?
The good news is that Jython now supports a large majority of the standard Python library. The bad news is that this has moved so rapidly, it's hard to keep the documentation up to date.
Built-in modules (e.g. those that are written in C for CPython) are a different story. These would have to be ported to Java, or implemented with a JNI bridge in order to be used by Jython. Some built-in modules have been ported to JPython, most notably cStringIO, cPickle, struct, and binascii. It is unlikely that JNI modules will be included in Jython proper though.
If you want to use a standard Python module, just try importing it. If that works, you're probably all set. You can also do a dir() on the modules to check the list of functions it implements.
If there is some standard Python module that you have a real need for that doesn't work with Jython yet, please send us mail.
In other words, you can directly use Python modules from Jython, unless you're trying to use built-in modules, in which case you're stuck with whatever has been ported to Jython.
Check your jython sys.path . Make sure that the library you want to load are in this path.
Look at jython faq for more details.
You can refer here for the solution Importing python modules in jython
Download ez_setup.py from here http://peak.telecommunity.com/dist/ez_setup.py
Then run jython ez_setup.py <any module name>.
Running it on any folder path doesn't matter.
I could install pymysql with it, no problem.

Categories

Resources