I have a couple of python scripts whose methods I'd like to access from Java. These functions take a string as a parameter and also return a string. What would be a simple and effective way to do that?
My python codes don't run under jython. With jython, I get loads of errors for simple things like:
'with' will become a reserved keyword in Python 2.6
However, I am able to create .so objects using cython. Could that be used from Java?
You will need to wrap the functions in a command line executable that outputs the desired result. Then you can use ProcessBuilder to call the script and get the output.
Related
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 want to create a function at run time in JAVA.
I want some thing like java Scripts equivalent of:
new Function([arg1[, arg2[, ...argN]],] functionBody)
So the user can specify function as a string, and thereafter can invoke it using the required parameters.
There are quite a few things in that direction in Java, but the closest would probably be the Java Scripting API which "is used to embed scripts in your Java applications".
NB if the code you want to execute is provided by the user, make sure you execute it in a sandboxed environment, otherwise you'll be hurt.
I want user to be able to input a snippet of code with a class or function implemented in Java and the program should run this code, without referring to external tools like javac or saving class files to fisk.
In Clojure it is clear, as there is read and eval. Can it be done for a plain Java code, possibly with some third party jar?
The code is expected to be some variables, some loops, some mathematical functions and maybe some calls to the framework that is running it.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert String to code in Java
Dynamic code execution on Java
I have a String containing : "for(int i=0 ; i<5 ; i++){System.out.println(\"*\");}"
Can I execute the code in this String in Java?
Since Java 6, you can compile and run a Java compilation unit defined as a String or a File using standard APIs in the SDK (a compilation unit is basically everything that goes inside a .java file - package, imports, classes/interfaces/enumerations), take a look at this example. You can't run an arbitrary Java snippet like the one in your question, though.
If at all possible, it'd be a better idea to embed a different scripting language that allows you to run snippets of code from a Java program - for example, JavaScript, Groovy, MVEL, BeanShell, etc.
If you turn it into a full-blown source file, you can feed it to the java compiler programmatically, but last time I checked that was only available if you had the java SDK installed on your machine; it was not available on machines with the client distribution of Java. Of course, this may have changed since then. Look at package com.sun.tools.javac and you will find the java compiler API there.
Maybe you can run this as Groovy:
http://groovy.codehaus.org/Embedding+Groovy
There isn't a Java Core API function for doing this, but you can call javac either by using Runtime.exec or using some "unsafe" classes from com.sun.tools.javac Here's an example:
http://juixe.com/techknow/index.php/2006/12/12/invoke-javac-at-runtime/
I don't think you can execute a String containing a java code.
But it is worth a try if you can save that as a java source file and try to use ProcessBuilder class to execute.
Never tried it and not sure if it is best way to do it. So use it with caution :)
Good Luck!
Also found a similar post: Runtime class in java
No, you can not execute this code in your program.
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'