Jython 2.2 - java.lang.ExceptionInInitializerError when accessing class .getPackage() - java

I have a Java class myObj with a static block as shown:
static{
Class<myObj> klass = myObj.class;
log.info("\nClientAPIVersion : "+ klass.getPackage().getImplementationVersion());
}
I'm creating an instance of this class in jython 2.2.
When I run my python script using :
java -Dlog4j.configuration=file://${CLASSPATH}/log4j.xml -jar ~/jython_2.2/jython.jar test.py
I get an exception as shown:
File "test.py", line 42, in ?
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
java.lang.ExceptionInInitializerError: java.lang.ExceptionInInitializerError
I found online that java.lang.ExceptionInInitializerError occurs due to a crash in the static block.
When I remove the log.info, the python code executes correctly.
I think that klass.getPackage() might be null, but if so, it should simply print 'null' in the log. Why the exception ?
What is the source of the problem and how may I fix it ? Thanks

Related

How do I import singleton object from Scala package in Java?

I am trying to use ARIMA object (Scala), which is imported from a package, in my Java program. Although the compilation succeeds, meaning that ARIMA class is recognized during compilation, there is NoClassDefFoundError for the ARIMA object in runtime. ARIMAModel class has no problem with importing since it is a class.
Is there any way to use the Scala object from my Java program?
Here is the source code for the object in Scala package.
File: .../com/cloudera/sparkts/models/ARIMA.scala
package com.cloudera.sparkts.models
object ARIMA {
def autoFit(ts: Vector, maxP: Int = 5, maxD: Int = 2, maxQ: Int = 5): ARIMAModel = {
...
}
}
class ARIMAModel(...) {
...
}
Here is my Java code.
File: src/main/java/SingleSeriesARIMA.java
import com.cloudera.sparkts.models.ARIMA;
import com.cloudera.sparkts.models.ARIMAModel;
public class SingleSeriesARIMA {
public static void main(String[] args) {
...
ARIMAModel arimaModel = ARIMA.autoFit(tsVector, 1, 0, 1);
...
}
}
Here is the error.
Exception in thread "main" java.lang.NoClassDefFoundError: com/cloudera/sparkts/models/ARIMA
at SingleSeriesARIMA.main(SingleSeriesARIMA.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:729)
at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:185)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:210)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:124)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.lang.ClassNotFoundException: com.cloudera.sparkts.models.ARIMA
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 10 more
I am using Scala version 2.11.8 and Java 1.8
You need to supply the dependency having Arima object present to the spark cluster using --jars option as below-
spark-submit --jars <path>/<to>/sparkts-0.4.1.jar --class SingleSeriesARIMA target/simple-project-1.0.jar
This will pass the other dependency along with the application jar to be available at spark-runtime.
TO call ARIMA object from java use-
ARIMA$.MODULE$.autoFit(tsVector, 1, 0, 1);

Not able to run jpf command :JPF exception, terminating: error reading class java.lang.reflect.AnnotatedElement

I am getting the below error when I try to run the jpf basset program. I was able to build jpf-core and jpf-actor successfully. Did anyone encounter this before? Am I missing out something? Appreciate your response/comments. Thanks.
~/b/p/j/jpf-core  bin/jpf gov.nasa.jpf.actor.Basset pi.Driver 3
[SEVERE] JPF exception, terminating: error reading class java.lang.reflect.AnnotatedElement
gov.nasa.jpf.classfile.ClassFileException: illegal constpool tag
at gov.nasa.jpf.classfile.ClassFile.error(ClassFile.java:168)
at gov.nasa.jpf.classfile.ClassFile.parseCp(ClassFile.java:1009)
at gov.nasa.jpf.classfile.ClassFile.parse(ClassFile.java:827)
at gov.nasa.jpf.jvm.ClassInfo.<init>(ClassInfo.java:803)
at gov.nasa.jpf.jvm.ClassInfo.loadClass(ClassInfo.java:1221)
at gov.nasa.jpf.jvm.ClassInfo.getResolvedClassInfo(ClassInfo.java:1207)
at gov.nasa.jpf.jvm.ClassInfo.loadInterfaceRec(ClassInfo.java:1994)
at gov.nasa.jpf.jvm.ClassInfo.loadInterfaceRec(ClassInfo.java:2000)
at gov.nasa.jpf.jvm.ClassInfo.getAllInterfaces(ClassInfo.java:2150)
at gov.nasa.jpf.jvm.JVM.registerStartupClass(JVM.java:531)
at gov.nasa.jpf.jvm.JVM.registerStartupClasses(JVM.java:510)
at gov.nasa.jpf.jvm.JVM.initialize(JVM.java:301)
at gov.nasa.jpf.JPF.run(JPF.java:616)
at gov.nasa.jpf.JPF.start(JPF.java:190)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at gov.nasa.jpf.tool.Run.call(Run.java:76)
at gov.nasa.jpf.tool.RunJPF.main(RunJPF.java:100)
---------------------- JPF error stack trace ---------------------
gov.nasa.jpf.JPFException: error reading class java.lang.reflect.AnnotatedElement
at gov.nasa.jpf.jvm.ClassInfo.loadClass(ClassInfo.java:1231)
at gov.nasa.jpf.jvm.ClassInfo.getResolvedClassInfo(ClassInfo.java:1207)
at gov.nasa.jpf.jvm.ClassInfo.loadInterfaceRec(ClassInfo.java:1994)
at gov.nasa.jpf.jvm.ClassInfo.loadInterfaceRec(ClassInfo.java:2000)
at gov.nasa.jpf.jvm.ClassInfo.getAllInterfaces(ClassInfo.java:2150)
at gov.nasa.jpf.jvm.JVM.registerStartupClass(JVM.java:531)
at gov.nasa.jpf.jvm.JVM.registerStartupClasses(JVM.java:510)
at gov.nasa.jpf.jvm.JVM.initialize(JVM.java:301)
at gov.nasa.jpf.JPF.run(JPF.java:616)
at gov.nasa.jpf.JPF.start(JPF.java:190)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at gov.nasa.jpf.tool.Run.call(Run.java:76)
at gov.nasa.jpf.tool.RunJPF.main(RunJPF.java:100)
Caused by: gov.nasa.jpf.classfile.ClassFileException: illegal constpool tag
at gov.nasa.jpf.classfile.ClassFile.error(ClassFile.java:168)
at gov.nasa.jpf.classfile.ClassFile.parseCp(ClassFile.java:1009)
at gov.nasa.jpf.classfile.ClassFile.parse(ClassFile.java:827)
at gov.nasa.jpf.jvm.ClassInfo.<init>(ClassInfo.java:803)
at gov.nasa.jpf.jvm.ClassInfo.loadClass(ClassInfo.java:1221)
... 15 more
~/b/p/j/jpf-core 
Don't know if it's worth but mine turn out to be a problem with the Java version. JPF library was compiled with Java 8 while I was trying to use it in Java 7. Matching the java version worked out for me.

Getting "java.lang.reflect.InvocationTargetException" error while trying to use 'protoc-1.0M4.jar'

I want to generate a protobuf mechanism for use in small embedded application.
I downloaded 'protoc-1.0M4.jar' from protobuf website, and trying to compile using java, my 'test.proto' file to generate C code. Here is the command I used and error I got:
Command :
$ java -jar protoc-1.0M4.jar test.proto
Exception :
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at edu.tum.cs.ccts.protobuf.embedded.EmbeddedCGenerator.getMaximumSerializedSize(EmbeddedCGenerator.java:491)
at edu.tum.cs.ccts.protobuf.embedded.EmbeddedCGenerator.compileHeader(EmbeddedCGenerator.java:307)
at edu.tum.cs.ccts.protobuf.embedded.EmbeddedCGenerator.doGenerate(EmbeddedCGenerator.java:113)
at edu.tum.cs.ccts.protobuf.embedded.Protoc.main(Protoc.java:126)
... 5 more
Can anyone suggest some guidelines please.
I tried the following example code provided in the website and it worked.
//File: phone.proto
//
//Optional Embedded C internal options:
//#max_repeated_length=100
//#max_string_length=32
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
required PhoneType type = 2;
}
I can try running, if you can provide your test.proto

Exception in thread "main" java.lang.NoClassDefFoundError: =

I created a new maven project in Eclipse and on runtime I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: =
Caused by: java.lang.ClassNotFoundException: =
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: =. Program will exit.
In other threads the class is mentioned where the problem occurs but here it simply says nothing.
The code is also used in a different project (with slight tweaks in terms of calling a method) but the rest of it is same.
If anyone can help me resolve this issue..it will be highly appreciated.
It looks like something is passing in = as the class name. It doesn't say nothing - it says =.
For example, when I run:
java =
I get:
Error: Could not find or load main class =
There's no colon, but it's otherwise the same.
Look at where you're trying to specify the class name, and see whether there's a stray = around. For example, suppose you had:
java -Dfoo = bar ClassName
instead of
java -Dfoo=bar ClassName
You'd see the same thing. I'm not familiar with Maven, but if you ever specify a set of arguments in it, I'd look at that part of the configuration file.
Deleting the workspace worked for me.

how to install .jar file onto zend server CE and call it from a php script

I have the zend javabridge working. Now I want to call a .jar file myWebTest.jar with an class of the same name. Then i want to call a function testWeb which returns a hello world string.
This in on 32bit Win 7 professional system
I added myWebTest.jar to the classpath located in /zend/zendserver/etc/java_bridge_server.ini:
[JAVA_BRIDGE_SERVER]
CLASSPATH="C:\Program Files\Zend\ZendServer\bin\javamw.jar;C:\Program Files\Zend\ZendServer\bin\myWebTest.jar;."
This path is correct.
I have restarted zend server and the code I am trying is:
$jObj = new Java("myWebTest");
// Print date through the object
print $jObj->testWeb("jim");
The log:
1 {main}
thrown in C:\Program Files\Zend\Apache2\htdocs\javaObject.php on line 4
[08-Dec-2011 11:44:48] PHP Fatal error: Call to a member function testWeb() on a non-object in C:\Program Files\Zend\Apache2\htdocs\javaObject.php on line 6
[08-Dec-2011 11:45:51] PHP Fatal error: Uncaught exception 'JavaException' with message 'Java Exception java.lang.ClassNotFoundException: myWebTest
java.lang.ClassNotFoundException: myWebTest
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
in C:\Program Files\Zend\Apache2\htdocs\javaObject.php:4
Stack trace:
#0 C:\Program Files\Zend\Apache2\htdocs\javaObject.php(4): *No Class!*->jbridge('myWebTest')
#1 {main}
thrown in C:\Program Files\Zend\Apache2\htdocs\javaObject.php on line 4
I can't find a how to on this anywhere. TIA Jim
From the documentation of Java
object java(string $class_name [ , ... ])
Parameters
class_name: Class name to create
...: Additional arguments are treated as constructor parameters
Return Value: The Java object that was created, NULL otherwise
Not to be confused, actually class name needs to be the full qualified Name of the class. As an example, if you have a class as below:
package experiment;
public class Test {
....
}
Here class name is: Test
Fully qualified name: experiment.Test
So to instantiate this class in PHP, you need to write:
$test = new Java("experiment.Test");
Additionally according to your exception log
Java Exception java.lang.ClassNotFoundException: myWebTest
It shows that, it can not find any class named as myWebTest in your class path. Which means either there is no class named myWebTest in the jar file, or jar is not properly loaded.
The argument to Java() is the fully qualified class name of the class you want to access, not the name of the jar file. For example the php java bridge docs say,
java("java.lang.Long")->MAX_VALUE
This access the class Long, in the package java.lang
There is some info on java packages and class names here.

Categories