Calling WSH from Java - java

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 ?

Related

Is there some Java equivalent of javaScripts **new Function([arg1[, arg2[, ...argN]],] functionBody)**

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.

Java calling python function with tensorflow graph

So I have a neural network in tensorflow (python2.7) and I need to retrieve its output using Java. I have a simple python function getValue(input) which starts the session and retrieves the value. I am open to any suggestions. I believe Jython wont work cause tensorflow is not in the library. I need the call to be as fast as possible. JNI exists for Java calling C so can I convert with cython and compile then use JNI? Is there a way to pass the information in RAM or some other way I haven't thought of?
In Python, save the model (using saver.save) and the graph (using tf.train.write_graph).
In Java, use the org.bytedeco.javacpp-presets library to instantiate a GraphDef from the saved protobuf file and pass in your input features and get the output features within a Session.
See https://medium.com/google-cloud/how-to-invoke-a-trained-tensorflow-model-from-java-programs-27ed5f4f502d#.4su1s26fz for example code.
I've had the same problem, Java+Python+TensorFlow. I've ended up setting up a simple http server. If that's too slow for you, you can shave off some overhead by employing sockets directly.
Encapsulate your calling for TensorFlow into a script.py and then:
Process proc = Runtime.getRuntime().exec("python script.py");
Not sure whether it solves your case.

Is it possible to install Java compiler in a database?

In order to make an online compiler, I want to compile a piece of code and send back the result.
Instead of giving the path to the hard disk, can I call a query which in return compiles the code (not by giving any links to javac hard disk location) but the files located in DB (BLOB).
Is it possible?
Is it OK to follow this approach ?
What system online compilers does usually follow?
Most databases allow you to create user defined functions. You could define such a UDF taking source code as the input and returning object code as the output.
This seems kind of pointless though since you are pushing this non-analytical computation into the database which is not designed yo do such things, whereas pulling the source from the database and writing the object code back is likely as efficient and much easier to implement and maintain.

Simple and effective way to call Python from Java

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.

How to manage Linux file permissions in Java?

Is there any mechanism to get and set the file/directory permissions?
For example, I want to show the permisssions of a file in a shell way:
-rwxr-xr--
Is it possible to do this using Java?
I know that there are some methods in the File class to know if the file canExecute, canRead and canWrite, but AFAIK this info is for the current user only. I need to know the whole octal number, for example 755, so I need to get it from the user, from group and from others.
I know that Java7 brings Posix operations, but how could do this using a smaller JRE?
I would like not to use a command like ls, or chmod.
If you can use external libraries, there are several:
JPosix
Posix for java
jnr-posix
If an entire library seems a hassle, creating a JNI wrapper that calls the lstat C function and returns the access mode takes you about 10 minutes. Here's a tutorial that creates such a wrapper for the isatty and ttyname functions.
As you say, in Java7, the JVM supports it, so you have a guarantee that this can be done portably in all OSs (because the JVM implementation takes care of it). Under Java7, you'd have to use a native library per OS you want to support. This is potentially even dirtier than executing chmod

Categories