java using jython has wrong python version - java

Im trying to use java to run a python function through jython.jar. I'm using a python module that is downloaded from web which needs python 2.6 or higher. I'm pretty sure that my python is version 2.7.2. However, when I try to run the java program, it continues to report python 2.5 detected. How can I resolve this?
My code is:
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
...
//cmd
String command = "\"xml\\" + name2 + "\"";
PythonInterpreter python = new PythonInterpreter(null, new PySystemState());
PySystemState sys = Py.getSystemState();
sys.path.append(new PyString("C:\\Python27\\Lib\\site-packages"));
python.execfile("work1.py");
}
}
}
}
And the error is:
Exception in thread "main" Traceback (most recent call last): File "work1.py", line 8, in <module>
import networkx as nx File "C:\Python27\Lib\site-packages\networkx\__init__.py", line 39, in <module>
raise ImportError(m % sys.version_info[:2]) ImportError: Python version 2.6 or later is required for NetworkX (2.5 detected). Java Result: 1

You need to use Jython 2.7b3, 2.7 beta adds language compatibility with CPython 2.7...
http://comments.gmane.org/gmane.comp.lang.jython.devel/6145
2.7 beta 4 will come in July and after that a feature freeze. I suggest you join the python-dev mailing list to stay aware of new releases. Hope this helps

Related

GraalVm Error: libgfortran.so.3 for running R code in Node (Ubuntu)

I am running a Node application using GraalVM. My Node code contains both R and Java interop code.
I installed R using GraalVM Updater:
gu install r
Node program:
const express = require('express')
const app = express()
const BigInteger = Java.type('java.math.BigInteger')
app.get('/', function (req, res) {
var text = 'Hello World from Graal.js!<br> '
// Using Java standard library classes
text += BigInteger.valueOf(10).pow(100)
.add(BigInteger.valueOf(43)).toString() + '<br>'
// Using R methods to return arrays
text += Polyglot.eval('R',
'ifelse(1 > 2, "no", paste(1:42, c="|"))') + '<br>'
// Using R interoperability to create graphs
text += Polyglot.eval('R',
`svg();
require(lattice);
x <- 1:100
y <- sin(x/10)
z <- cos(x^1.3/(runif(1)*5+10))
print(cloud(x~y*z, main="cloud plot"))
grDevices:::svg.off()
`);
res.send(text)
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
Console output:
Example app listening on port 3000!
FastR unexpected failure: error loading libR from: /.sdkman/candidates/java/20.1.0.r11-grl/languages/R/lib/libR.so.
Message: libgfortran.so.3: cannot open shared object file: No such file or directory
Troubleshooting:
* Please run /home/.sdkman/candidates/java/20.1.0.r11-grl/languages/R/bin/configure_fastr. It will check that your system has the necessary dependencies and if not it will suggest how to install them.
* If this does not help, please open an issue on https://github.com/oracle/fastr/ or reach us on https://graalvm.slack.com.
/home/server.js:19
text += Polyglot.eval('R',
... other logs
I have also installed build-essential, gfortran, libxml2 and libc++-dev as suggested by GraalVm compiler.
Is it an issue with GraaLVM or Ubuntu FastR package installation does by GraalVM?
Please try again with the latest version of GraalVM. 20.1.0 is already several months old; if you try again with 20.3.0 you should get lots of bugfixed that where included in the newer version.
Also, note that the error message even tells you to run a specific command on your shell, /home/.sdkman/candidates/java/20.1.0.r11-grl/languages/R/bin/configure_fastr. Did you do that? Did that help? What was the output of that, was there an error?
Thanks,
Christian

Google OR-Tools in Intellij: UnsatisfiedLinkError

I am setting up a java framework that should use the Google OR-Tools. The code below compiles successfully, but throws an exception at runtime:
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.google.ortools.linearsolver.operations_research_linear_solverJNI.MPSolver_CLP_LINEAR_PROGRAMMING_get()I
at com.google.ortools.linearsolver.operations_research_linear_solverJNI.MPSolver_CLP_LINEAR_PROGRAMMING_get(Native Method)
at com.google.ortools.linearsolver.MPSolver$OptimizationProblemType.<clinit>(MPSolver.java:221)
at Main.main(Main.java:15)
I am using Intellij 2018.3 on Windows 10. I spent a lot of time trying to get this run, but unsuccessful. Based on what I found on the internet, the exception might be caused by poor linking and/or missing external libraries on which OR-Tools depends. However, I don't have the background to resolve this issue, and also Intellij does not highlight anything. Any idea what the problem is?
For completion, this is the code I run:
import com.google.ortools.linearsolver.MPObjective;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;
public final class Main {
public static void main(String[] args) {
// Create the linear solver with the GLOP backend.
MPSolver solver =
new MPSolver("SimpleLpProgram", MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
// Create the variables x and y.
MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
MPVariable y = solver.makeNumVar(0.0, 2.0, "y");
System.out.println("Number of variables = " + solver.numVariables());
// Create a linear constraint, 0 <= x + y <= 2.
MPConstraint ct = solver.makeConstraint(0.0, 2.0, "ct");
ct.setCoefficient(x, 1);
ct.setCoefficient(y, 1);
System.out.println("Number of constraints = " + solver.numConstraints());
// Create the objective function, 3 * x + y.
MPObjective objective = solver.objective();
objective.setCoefficient(x, 3);
objective.setCoefficient(y, 1);
objective.setMaximization();
solver.solve();
System.out.println("Solution:");
System.out.println("Objective value = " + objective.value());
System.out.println("x = " + x.solutionValue());
System.out.println("y = " + y.solutionValue());
}
}
In my case solution was simple - I just needed to add this singe line of code:
Loader.loadNativeLibraries();
where loader comes from com.google.ortools.Loader
Disclaimer: more a long comment than an answer...
note: I supposed you are using the github repository of or-tools if you used the binary package it should be more or less the same...
1) You must load the jni library which will load the OR-Tools C++ libraries and its dependencies...
/** Simple linear programming example.*/
public class Main {
static {
System.loadLibrary("jniortools");
}
public static void main(String[] args) throws Exception {
2) Did you manage to run the java samples ?
make run SOURCE=ortools/linear_solver/samples/SimpleLpProgram.java
ref: https://developers.google.com/optimization/introduction/java#simple_example
3) As Kayaman pointed out, you must pass the folder where the java runtime can find the native libraries (i.e. the JNI wrapper jniortools.dll and its dependencies libortools.dll)
if you look at the console log you'll see the full command line:
java -Xss2048k -Djava.library.path=lib -cp lib\sample.jar;lib\com.google.ortools.jar;lib\protobuf.jar ...\sample
Which comes from, the makefiles/Makefile.java file:
JAVAFLAGS = -Djava.library.path=$(LIB_DIR)
...
ifeq ($(SOURCE_SUFFIX),.java) # Those rules will be used if SOURCE contain a .java file
$(CLASS_DIR)/$(SOURCE_NAME): $(SOURCE) $(JAVA_OR_TOOLS_LIBS) | $(CLASS_DIR)
-$(DELREC) $(CLASS_DIR)$S$(SOURCE_NAME)
-$(MKDIR_P) $(CLASS_DIR)$S$(SOURCE_NAME)
"$(JAVAC_BIN)" -d $(CLASS_DIR)$S$(SOURCE_NAME) \
-cp $(LIB_DIR)$Scom.google.ortools.jar$(CPSEP)$(LIB_DIR)$Sprotobuf.jar \
$(SOURCE_PATH)
...
.PHONY: run # Run a Java program.
run: build
"$(JAVA_BIN)" -Xss2048k $(JAVAFLAGS) \
-cp $(LIB_DIR)$S$(SOURCE_NAME)$J$(CPSEP)$(LIB_DIR)$Scom.google.ortools.jar$(CPSEP)$(LIB_DIR)$Sprotobuf.jar \
$(SOURCE_NAME) $(ARGS)
endif
src: https://github.com/google/or-tools/blob/46173008fdb15dae1dca0e8fa42a21ed6190b6e4/makefiles/Makefile.java.mk#L15
and
https://github.com/google/or-tools/blob/46173008fdb15dae1dca0e8fa42a21ed6190b6e4/makefiles/Makefile.java.mk#L328-L333
note: you can run make detect_java to know the flags i.e. value of LIB_DIR
note: if you did use the precompiled package the Makefile is here:
https://github.com/google/or-tools/blob/stable/tools/Makefile.cc.java.dotnet
Then after you can try to add this option in Intellij...
You must understand that or-tools is a set of C++ native libraries which are wrapped to Java using the SWIG generator.
To make it work using Intellij (over a windows machine) you need to:
Install Microsoft Visual C++ Redistributable for Visual Studio
Download and extract the OR-Tools library for Java
In intellij, add jar dependency to the 2 jars under the lib folder of the extracted files (each of the 2 jars separately, do not add to lib folder itself. This is why).
Add the lib library path to VM options. In Intellij edit your run-configuration and add to vm options: -Djava.library.path=<path to the lib folder that hold the jars>
Load the jni library statically by adding the below code to your class (as mentioned here.)
static {
System.loadLibrary("jniortools");
}

Apache tomcat | py4j | java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens

I am using py4j to call Java methods from my python code. When I start the java gateway on eclipse and use python code to call the java methods I get proper response. But when I deploy the same java program as war file on tomcat and then use python code to call the java methods deployed on tomcat I get the following errors.
Java code:
package testjav;
import py4j.GatewayServer;
public class Helloworld {
public String sayhello() {
System.out.println("Hi there");
return "hi";
}
public static void main(String[] args) {
GatewayServer gatewayServer = new GatewayServer(new Helloworld());
gatewayServer.start();
System.out.println("Gateway Server Started");
}
}
Python Code:
from py4j.java_gateway import JavaGateway, GatewayParameters
gateway = JavaGateway(gateway_parameters=GatewayParameters(port=8080))
out = gateway.entry_point.sayhello()
print(out)
Error on Tomcat:
Error on Python:
Traceback (most recent call last):
File "C:\Users\******\Documents\*****\helloworld\tasksHello\tasks.py", line 24, in <module>
out = gateway.entry_point.sayhello()
File "C:\Users\******\AppData\Local\Continuum\anaconda3\lib\site-packages\py4j\java_gateway.py", line 1160, in __call__
answer, self.gateway_client, self.target_id, self.name)
File "C:\Users\*******\AppData\Local\Continuum\anaconda3\lib\site-packages\py4j\protocol.py", line 318, in get_return_value
value = OUTPUT_CONVERTER[type](answer[2:], gateway_client)
KeyError: 'T'
I checked this answer variable by looking into py4j module code.
In case of jvm run it returns : yshi which is expected as hi is output and ys are used by py4j. But in case of tomcat answer = 'HTTP/1.1'. I don't understand why this is happening. Please guide me.

readFully not defined with Java Nashorn Javascript Engine

I am trying to run a javascript script with the new Java 8 Nashorn javascript engine but it fails with the following error:
<eval>:1 ReferenceError: "readFully" is not defined
The script uses the readFully function that should be defined in the global scope nashorn is run with the scripting mode enabled (wich is default when running through a ScriptEngine as seen here http://mail.openjdk.java.net/pipermail/nashorn-dev/2013-December/002562.html).
Here is a sample to reproduce the error:
import java.io.FileNotFoundException;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Test {
public static void main(String[] argv) throws FileNotFoundException, ScriptException {
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("nashorn");
scriptEngine.eval("print('Hey!');print(print);print(readFully);");
}
}
This sample prints Hey ! and then the source code of the print function (another nashorn built-in function) and finally it should print the source code of the readFully method. But I have this Exception instead:
Exception in thread "main" javax.script.ScriptException: ReferenceError: "readFully" is not defined in <eval> at line number 1
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:586)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:570)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:525)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:521)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:192)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
at com.github.bringking.maven.requirejs.Test.main(Test.java:14)
Caused by: <eval>:1 ReferenceError: "readFully" is not defined
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:58)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:320)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:292)
at jdk.nashorn.api.scripting.NashornScriptEngine.__noSuchProperty__(NashornScriptEngine.java:272)
at jdk.nashorn.internal.scripts.Script$engine.L:35(nashorn:engine/resources/engine.js:37)
at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:1)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:535)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:568)
... 5 more
When the sample script is run with the nashorn command line with the -scripting parameter (with the jjs tool of the jdk), all is fine. Here is the result of the same script:
Hey!
function print() { [native code] }
function readFully() { [native code] }
I could rewrite a readFully method and bind it with the script context, but I prefer to understand why it does not work and use already built-in functions.
Regards
Finally, I have implemented a readFully function that I use in my script (Only compatible with Nashorn):
function readFully(url) {
var result = "";
var imports = new JavaImporter(java.net, java.lang, java.io);
with (imports) {
var urlObj = null;
try {
urlObj = new URL(url);
} catch (e) {
// If the URL cannot be built, assume it is a file path.
urlObj = new URL(new File(url).toURI().toURL());
}
var reader = new BufferedReader(new InputStreamReader(urlObj.openStream()));
var line = reader.readLine();
while (line != null) {
result += line + "\n";
line = reader.readLine();
}
reader.close();
}
return result;
}
readFully is not a standard JavaScript function and it is likely not standard in Nashorn either.
There were similar issues when Rhino was chosen for inclusion in the Sun implementation of Java 6. The scripting tool may provide enhancements that are not present in the embedded API. readFully is not a documented function in the Java 8 Nashorn API.
In previous versions of Java the specification stated that provided scripting engines were an implementation detail of the JRE vendor. I am not aware if Java 8 makes anything about the engines provided mandatory or whether it makes any future compatibility guarantees. I would check JSR-337 thoroughly if this was likely to be an issue.
https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions
readFully (-scripting mode only)
This function reads the entire contents of a file passed in as a string argument and sends it to stdout, or you can assign the result to a variable.
readFully example:
jjs> readFully("text.txt")
This is the contents of the text.txt file located in the current working directory.
readFully is enabled only in scripting mode. Nashorn docs
try this:
>>jjs -scripting
jjs> readFully("your_file")
readFully() function is only available as built in function in nashorgn when you enable scripting in you application
There are two ways you can do this:-
pass -Dnashorn.args=-scripting when you run your application to enable nashorn
scripting
Enable it programmatically by creating Nashorn engine with scripting args
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine(new String[] { "-scripting" });

Why does Jython not find this module? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to run a python script from java?
I am running a Python script using Jython and got this error:
Exception in thread "main" Traceback (innermost last): File "C:\Facebook\LoginPython\Facebook.py", line 5, in ? ImportError: no module named cookielib
Why doesn't this work?
A little bit more about using Jython - had my share of problems with that as well. Note that this may not be the best way to do it, but it works fine for me.
I assume you want to call a function foo in module bar from your Java code that takes a string argument and returns a string:
PythonInterpreter interpreter = new PythonInterpreter();
// Append directory containing module to python search path and import it
interpreter.exec("import sys\n" + "sys.path.append(pathToModule)\n" +
"from bar import foo");
PyObject meth = interpreter.get("foo");
PyObject result = meth.__call__(new PyString("Test!"));
String real_result = (String) result.__tojava__(String.class);
The sys.path.append() part is only necessary if your module isn't part of the Python search path by default, which may very well be the problem if you get Import or Module not find errors.
Also you need to cast the objects between the java and python versions, you'll need to look that up if necessary, so far I only needed primitive types that were easy to cast, not sure if it's as easy for arbitrary java objects.
Use Jython to run Python on the JVM. Use PyDev to develop with Python (or Jython) on Eclipse.

Categories