Java Nashorn compatibility file crashes - java

When I try to load the Nashorn compatibility file for Rhino (load("nashorn:mozilla_compat.js")) it comes up with the following error:
java.lang.RuntimeException: javax.script.ScriptException: ReferenceError: "net" is not defined in nashorn:mozilla_compat.js at line number 67
I've tried everything to get it to work but nothing has helped :(

This can happen if your script (not mozilla_compat.js itself) contains a declaration with a qualified name like this:
var x = new net.yourdomain.yourpackage.ClassName();
instead of doing
importPackage(Packages.net.yourdomain.yourpackage);
var x = new ClassName();
The former works in Rhino, but not in Nashorn, even with the compatibility script. The latter however will work in both environments.

I ran the following code with the latest JDK 8 update released (8u60) - available for download # http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
import javax.script.*;
public class Main {
public static void main(String[] ar) throws Exception {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = m.getEngineByName("nashorn");
e.eval("load('nashorn:mozilla_compat.js')");
// this should print 'function' and mozilla_compat.js defines that function
e.eval("print(typeof importClass)");
}
}
And it printed "function" as expected. I checked it on jdk9-dev tip forest build as well. It works with that version as well. Will you please print "java -version" and make sure you're using recent JDK 8 ?

Related

simple groovy program fails with error: No signature of method: java.lang.ProcessBuilder.inheritIO() is applicable for argument types: () values: []

I'm scratching my head on this one, The program is as follows:
class MyClass {
def static someMethod() {
def pb = new ProcessBuilder("")
pb.inheritIO()
pb.setCommand(/* command list */)
def process = pb.start()
...
println "profit"
}
}
except running the above (or the equivelant of) gives the output:
No signature of method: java.lang.ProcessBuilder.inheritIO() is applicable for argument types: () values: []
inheritIO is obviously a method defined in the ProcessBuilder class
so what is going wrong here?
CONTEXT: this is happening during a gradle build using jdk 7u55 except i imagine this info is unrelated. to me it looks like groovy has forgotten what it was doing.
EDIT: if i delete the pb.inheritIO() line then when i call pb.start() it throws another error:
java.lang.ArrayStoreException
Turns out the answer is quite involved but very specific to my setup:
although i have my org.gradle.java.home property set to the java 1.7 jdk, I am using a properties plugin that uses different property files according to an environment variable called environmentName this property was set to the incorrect value thus it was reading JAVA_HOME from my environment rather than the variable i set in the property file i wanted. I changed this and the jre was switched to the correct runtime.

Rcaller simple code - Error loading Runiversal

I have the follow Java code that uses Rcaller.
RCaller caller = new RCaller();
RCode code = new RCode();
caller.setRscriptExecutable("/usr/bin/Rscript");
code.addRCode("install.packages(\"bbmle\")");
caller.redirectROutputToConsole();
caller.runOnly();
Essentially, I'm trying to run Java code that installs an R package (because later I will run R code within Java that requires this package).
When I run this code, I get the following output in Java
Error:Loading required package: Runiversal
However, I do have the Runiversal package on my Mac. Any ideas what this means, and why I'm not actually seeing any R output of the install.packages method, which is normally quite verbose?
UPDATE: I should note that even simple code such as the following results in the same error:
code.addRCode("x = c(1,2,3)");
code.addRCode("y = c(2,3,5");
code.addRCode("x+y");
caller.redirectROutputToConsole();
caller.runOnly();
UPDATE: I should also mention that the following works great:
StringBuffer allCode = readFile("temp.R");
code.setCode(allCode);
double[] xvector = new double[]{1,3,5,3,2,4,5,6,7,8,9,21,22,25,27,25,34,39,31};
double[] yvector = new double[]{6,7,5,6,5,6,6,7,6,8,9,21,20,19,23,24,29,38,30};
code.addDoubleArray("X",xvector);
code.addDoubleArray("Y",yvector);
code.addRCode("fun(X,Y)");
temp.R:
fun = function(x,y) {
return(lm(y~x))
}
Java output:
Output:
Output:Call:
Output:lm(formula = y ~ x)
Output:
Output:Coefficients:
Output:(Intercept) x
Output: 2.445 0.825
Output:
R loads package from the libraries. If the library that you installed the Runiversal package into is not being searched by the R process started under Java, that could result in the error message you're seeing.
So: what is the directory path (i.e., library) that the Runiversal package is installed in? Possibly related to this is what user installed the R package, and what user is running the Java code.
The 2.2 version of RCaller library does not require the R package Runiversal. A compact version of R to XML converters are implemented in the package. Try it out here

Eclipse, change compiler command (for JOMP)

I want to use JOMP API (equivalent to OpenMP in C) but I met some problems:
This is the code I want to run:
import jomp.runtime.*;
public class Hello
{
public static void main (String argv[])
{
int myid;
//omp parallel private(myid)
{
myid = OMP.getThreadNum();
System.out.println("Hello from " + myid);
}
}
}
It is just an hello worl but I have a problem with the compiler. Please have a quick look at this page to understand:
http://www2.epcc.ed.ac.uk/computing/research_activities/jomp/download.html
But I can't, I do not understand how it works... I can only compile it with eclipse default compiler (I guess) and then I have only one thread!
I understand I have to compile this code (in a .jomp file) with
java jomp.compiler.Jomp MyFile
and then compile normally but I can't do this in ecplise neither in the terminal (I do not know how to install this compiler!)
ps: I use Ubuntu 12.04 on a Intel® Core™ i7-3610QM CPU # 2.30GHz × 8.
You just need to add the JOMP parameters to your launch configuration, this example can help you:
JOMP eclipse workaround

Syntax error in javax.xml.xpath.XPathFactory provider-configuration file of Saxon-HE 9.3

I am using Java SE 6 on Mac OS X and Saxon-HE 9.3.0.5. The ServiceLoader is not able to find the Saxon implementation of javax.xml.xpath.XPathFactory.
mac:test2 ludo$ java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03-383-11A511)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-383, mixed mode)
The javadoc of the newInstance method of javax.xml.xpath.XPathFactory states in point 3 of the lookup procedure to localize an implementation that:
The class loader is asked for service provider provider-configuration files matching javax.xml.xpath.XPathFactory in the resource directory META-INF/services. See the JAR File Specification for file format and parsing rules.
The Service Provider section of the JAR File Specification states that:
The file should contain a newline-separated list of unique concrete provider-class names.
But if I extract the saxon9he.jar file and look into the META-INF directory, I see:
mac:Java ludo$ mkdir test
mac:Java ludo$ cd test
mac:test ludo$ jar fx ../saxon9he.jar
mac:test ludo$ cat META-INF/services/javax.xml.xpath.XPathFactory
net.sf.saxon.xpath.XPathFactoryImpl
http\://java.sun.com/jaxp/xpath/dom: net.sf.saxon.xpath.XPathFactoryImpl
http\://saxon.sf.net/jaxp/xpath/om: net.sf.saxon.xpath.XPathFactoryImpl
The first line is correct but I can't see why there are two extra lines and it looks like those lines are causing trouble to ServiceLoader. I saw the problem with a test example that I wrote the understand the mecanism used to find a provider. We can see that saxon9he.jar is in the CLASSPATH.
mac:services ludo$ java ServicesTest
CLASSPATH = ..., /Users/ludo/Library/Java/saxon9he.jar, ...
Service XPathFactory: java.util.ServiceLoader[javax.xml.xpath.XPathFactory]
ServiceConfigurationError: javax.xml.xpath.XPathFactory: jar:file:/Users/ludo/Library/Java/saxon9he.jar!/META-INF/services/javax.xml.xpath.XPathFactory:2: Illegal configuration-file syntax
The line of interest is:
jar:file:/Users/ludo/Library/Java/saxon9he.jar!/META-INF/services/javax.xml.xpath.XPathFactory:2: Illegal configuration-file syntax
Is it a bug of Saxon or an extended syntax not supported by my system ? What could i do to solve the issue ?
Note that if I explicitly choose the class for the implementation, I can get a factory. But I want to use the Services mechanism. The following code works:
XPathFactory xpf = XPathFactory.newInstance(
XPathFactory.DEFAULT_OBJECT_MODEL_URI,
"net.sf.saxon.xpath.XPathFactoryImpl",
ClassLoader.getSystemClassLoader());
I have added the whole Java test program below.
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Iterator;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import javax.xml.xpath.XPathFactory;
public class ServicesTest {
public static String getClasspathString() {
StringBuilder classpath = new StringBuilder();
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader) classLoader).getURLs();
for (int i = 0; i < urls.length - 1; i++) {
classpath.append(urls[i].getFile()).append(", ");
}
if (urls.length > 0) {
classpath.append(urls[urls.length - 1].getFile());
}
return classpath.toString();
}
public static void availableProviders(ServiceLoader sl) {
Iterator it = sl.iterator();
int index = 0;
for (;;) {
try {
if (!it.hasNext()) {
break;
}
index++;
Object o = it.next();
System.out.printf("%03d Concrete class name: %s\n", index, o.getClass().getName());
} catch (ServiceConfigurationError e) {
System.err.printf("ServiceConfigurationError: %s\n", e.getMessage());
}
}
}
public static void main(String[] args) {
System.out.printf("CLASSPATH = %s\n", getClasspathString());
System.out.println();
ServiceLoader<XPathFactory> slXPathFactory = ServiceLoader.load(XPathFactory.class);
System.out.printf("Service XPathFactory: %s\n", slXPathFactory.toString());
availableProviders(slXPathFactory);
}
}
Michael Kay answered the question on a SourceForge forum. He said that:
The format of the file was chosen to circumvent a JDK5 bug.
And also that:
Actually, I wouldn't recommend using the JAXP search mechanism anyway. It's very slow, and it delivers an XPath engine that won't necessarily work with your application. You have no way of knowing whether you get an XPath 1.0 or 2.0 implementation back, and the API is so weakly defined that there's very little chance your application will work with a particular provider unless you have tested it with that provider first. So even without this bug, I would steer clear of it.
I think it answers the question even if it does not provide an explicit fix for the problem. So we could chose the implementation by writing:
XPathFactory xpf = XPathFactory.newInstance(
XPathFactory.DEFAULT_OBJECT_MODEL_URI,
"net.sf.saxon.xpath.XPathFactoryImpl",
ClassLoader.getSystemClassLoader());
I know this is an older thread, but this post of mine might shed some light on the issue. It finds the XPathFactory using a -D parameter that I've never seen documented.

Accessor method visible under Windows, Linux, but not OS X

Trying to build against javax.vecmath using the 1.5.2 jar file (found on Java.net http://java3d.java.net/binary-builds.html, for example).
Try to make a call on, say Point3d;
public class Foo {
public static void main(String[] args) {
Point3d t = new Point3d(1.0, 1.0, 1.0);
System.out.println(t.getX());
}
}
In 64 bit Windows and Linux (I've only tried Ubuntu 10.04, 64 bit), this compiles and runs.
In OS X (10.6.7) it will not compile:
...: cannot find symbol
symbol : method getX()
location: class javax.vecmath.Point3d
System.out.println (t.getX());
This is using the exact same physical vecmath.jar
If instead I use the source directly, it compiles on OS X, but does not run
Exception in thread "main" java.lang.NoSuchMethodError: javax.vecmath.Point3d.getX()D
If I compile the sources myself on OS X to a jar file, and then use the jar in the example above, again, unable to compile.
Now, the fields being accessed are in javax.vecmath.Tuple3d, which is an abstract class with public fields for x, y and z. So on OS X this will work (actually, it seems to work everywhere).
public class Foo {
public static void main(String[] args) {
Point3d t = new Point3d(1.0, 1.0, 1.0);
System.out.println(t.x);
}
}
The thing is, I'm working on a code base that depends on vecmath.jar, and in which the maintainers are on Windows and wish to keep using the accessor methods, but I'm on OS X.
I'm looking to both:
(1) understand what is going on
(2) figure out how to make these sources portable while depending on the vecmath.jar file.
The "Exception in thread "main" java.lang.NoSuchMethodError: javax.vecmath.Point3d.getX()" indicates that not the 1.5.2 version but the Apple version 1.3 of vecmath.jar is accessed. The *getter" and "setter" methods were introduced in 1.5.
Check if Apple's out-dated Java 3D version 1.3 is installed in System/Library/Java/Extensions/ on your Mac. Remove all Java 3D 1.3 related files including vecmath.jar (jar, jnilib), they are useless.
August, InteractiveMesh

Categories

Resources