Include .jar-File in PHPJAVA Bridge - java

I try to load the "Tasks" from a .mpp-File.
If I get the current date by java.util.Date it works very fine, to access JAVA but if i try to access the MPPReader I get the error:
CreateInstance failed: new net.sf.mpxj.mpp.MPPReader. Cause: java.lang.ClassNotFoundException
This is the code to my test case.
define("JAVA_SERVLET", false);
define("JAVA_HOSTS", 9267);
$mpxjPath = getcwd()."/libs/java/mpxj-5.6.0.jar";
exec("java -cp $mpxjPath");
require_once("libs/java/Java.inc");
$date = new java('java.util.Date');
echo $date;
$project = new java('net.sf.mpxj.mpp.MPPReader');
This is the output in the browser
Wed Feb 01 08:53:43 CET 2017
Fatal error: Uncaught [[o:Exception]:"java.lang.Exception: CreateInstance failed: new net.sf.mpxj.mpp.MPPReader. Cause: java.lang.ClassNotFoundException: net.sf.mpxj.mpp.MPPReader VM: 1.7.0_55#http://java.oracle.com/" at:
#-15 java.net.URLClassLoader$1.run(URLClassLoader.java:366)
#-14 java.net.URLClassLoader$1.run(URLClassLoader.java:355)
...
The output shows, that the java is reachable. But how can I access the MPPReader ?
Update
Server: CentOS 7
This is the code in my index.php right now.
<?php
define("JAVA_HOSTS", 9267);
define("JAVA_SERVLET", false);
require_once("libs/java/Java.inc");
echo "<pre>";
$date = new java('java.util.Date');
echo $date;
$project = new java('net.sf.mpxj.mpp.MPPReader');
?>
Run Standalone command on CentOS 7
java -cp "ext/JavaBridge.jar:libs/java/mpxj-5.6.0.jar" php.java.bridge.Standalone SERVLET:9267
This is the error.
Thu Feb 02 07:11:45 CET 2017PHP Fatal error: Uncaught [[o:Exception]:"java.lang.Exception: CreateInstance failed: new java.net.sf.mpxj.mpp.MPPReader. Cause: java.lang.ClassNotFoundException: java.net.sf.mpxj.mpp.MPPReader VM: 1.7.0_55#http://java.oracle.com/" at:
#-15 java.net.URLClassLoader$1.run(URLClassLoader.java:366)
#-14 java.net.URLClassLoader$1.run(URLClassLoader.java:355)
#-13 java.security.AccessController.doPrivileged(Native Method)
#-12 java.net.URLClassLoader.findClass(URLClassLoader.java:354)
#-11 java.lang.ClassLoader.loadClass(ClassLoader.java:425)
#-10 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
#-9 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
#-8 java.lang.Class.forName0(Native Method)
#-7 java.lang.Class.forName(Class.java:270)
#-6 php.java.bridge.Util.classForName(Util.java:1518)
#-5 php.java.bridge.JavaBridge.CreateObject(JavaBridge.java:445)
#-4 php.java.bridge.Request.handleRequest(Request.java:458)
#-3 php.java.bridge.Request.handleRequests(Request.java:500)
#-2 php.java.bridge.http.ContextRunner.run(Co in http://127.0.0.1:9267/JavaBridge/java/Java.inc on line 195

Looking at your attempt to load the mpxj-5.6.0.jar, I noticed you tried to make it available to the JVM from the client side (PHP):
$mpxjPath = getcwd()."/libs/java/mpxj-5.6.0.jar";
exec("java -cp $mpxjPath");
require_once("libs/java/Java.inc");
The idea looks appealing, but sadly cannot work that way.
Remember the java-bridge is made of two parts, the client (Java.inc / PHP) and server (PHPJavaBridge / Generally a servlet). So if you want to add a library to the classpath, it must be done on the server side (available to the JVM).
From your code example (define("JAVA_SERVLET", false)), I assume you're not starting the bridge through Tomcat/servlet env, but with the standalone server. In this scenario, you'll have to add the *.jar on the classpath when starting the server.
You'll end up doing something approx like this to start the server :
$ java -cp "./JavaBridge.jar:/path/to/mpxj-5.6.0.jar" php.java.bridge.Standalone SERVLET:9267
edit: replace /path/to/ in by the correct location of the jar.
If you're working on Linux/Mac, have a look at the pjbserver-tools project. It provides a php-wrapper around the standalone server where you can easily configure deps.
Just keep aware of two things:
Servlet mode (.war/tomcat) is the way to go if you intend to use it in production. If you're unsure about how it works, see the pjb-starter-gradle which contains documentation.
The older Java.inc won't run on PHP7 and support won't probably be continued... At least take time to consider using the reworked client: soluble-japha. It might even be easier to work with it, but of course, as the author, I'm biased. Check for yourself ;)
Note for servlet tomcat:
For servlet tomcat install, same idea applies. Just make sure your .jar file(s) are present in WEB-INF\libdirectory. See also the unoffical php-java-bridge fork to get the latest .war template.

Related

GATE Java error

I know almost nothing about Java so please take it easy on me. I'm using this plugin, which I've had working nicely for a few days on my Mac (following the repo's very simple instructions below), but when I took it to an Ubuntu instance I got the following error:
ResumeParser/ResumeTransducer$ java -cp 'bin/*:../GATEFiles/lib/*:../GATEFILES/bin/gate.jar:lib/*' code4goal.antony.resumeparser.ResumeParserProgram somefile.pdf somefile.json
Exception in thread "main" java.lang.NoClassDefFoundError: gate/SimpleAnnotation
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
at java.lang.Class.getMethod0(Class.java:2866)
at java.lang.Class.getMethod(Class.java:1676)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: gate.SimpleAnnotation
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more
Anyone have thoughts on what is going wrong?
Installation and usage instructions:
1. git clone https://github.com/antonydeepak/ResumeParser.git
2. cd ResumeParser/ResumeTransducer
3. export GATE_HOME="..\GATEFiles"
Paths are case sensitive in Ubuntu, GATEFiles is different from GATEFILES. Since gate.SimpleAnnotation is in gate.jar, I bet that your classpath should be:
-cp 'bin/:../GATEFiles/lib/:../GATEFiles/bin/gate.jar:lib/*'
Disclaimer: I haven't used this plugin, I don't know if the other parts of the classpath are correct.
In general, you can "debug" by trying
ls ../GATEFiles/bin/
to see if there is a gate.jar file.
It's hard to know, but most likely the class it cannot find is in the gate.jar file, and it is either not on the new machine, or is not in the correct place on the new machine.
To expand slightly: Classes are found in a couple of places, but you specify a gate.jar on the classpath of the command line (that's what the -cp designates), so I'm guessing it's there. Since java can't find it, I'm guessing further that the necessary jar file is not where it needs to be for the Java runtime to find it.
For Linux and OSX use '/' and ':' , try this call .. it worked for me
java -cp './bin/*:../GATEFiles/lib/*:../GATEFiles/bin/gate.jar:./lib/*' code4goal.antony.resumeparser.ResumeParserProgram cv.pdf cv.json

Runtime error loading Interface definition in Java using Apache HTTP library

I am attempting to make a simple wrapper around an API connection in Java. I am using the org.apache.http library to do this.
I have downloaded the following jar files:
org.apache.http httpclient saved as ./org.apache.httpclient.jar
org.apache.http httpcore saved as ./org.apache.httpcore.jar
I can then successfully compile the file with:
$ javac -cp "./org.apache.httpclient.jar:./org.apache.httpcore.jar" Api.java
However when running it with $ java Api I receive:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
at java.lang.Class.getMethod0(Class.java:2866)
at java.lang.Class.getMethod(Class.java:1676)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more
[It turned out the issue was with how code was being run after compilation; the code has been removed to avoid distracting from the actual problem and solution]
I can't see why the system has a problem finding the HttpEntity interface when it finds other imports successfully, and can compile. If I remove any of the import lines compilation fails with unknown symbol errors.
-cp should contain the directory where you classes are, for example:
java -cp "./org.apache.httpclient.jar:./org.apache.httpcore.jar:." Api
Assuming that your Api.class is in the current directory. (replace : by ; as classpath separator if your are on a windows system)
Have you tried running the java with '-classpath'. While compiling you are giving the classpath, so you need to add the classpath option for running the class file as well.
More details can be found at below link:
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
Also you can print which libs are in classpath using below command.
java -cp $(for i in lib/*.jar ; do echo -n $i: ; done). com.test.Class

Getting NoClassDefFoundError for an included jar

I'm writing a java app and running it on a linux ec2 server.
the program was running fluently yesterday, and after I changed and organazied some files and packages I'm getting this problem when I execute the program:
I'm running the programm with this command:
java -cp . main.Server
And get the following error:
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: org/json/JSONObject
at main.Group.<init>(Group.java:28)
at main.Server.run(Server.java:23)
Caused by: java.lang.ClassNotFoundException: org.json.JSONObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 2 more
I was running the programm as this before the changes:
export CLASSPATH=$CLASSPATH:/home/ec2-user/java-json.jar
In the bin folder, command:
java Server
And the programm was running.
Since I changed the package from default name to main, I need to run it with the command java -cp . main.Server, but I get the error written above.
I also tried modifing the .bash_profile and add this:
CLASSPATH=$CLASSPATH:/home/ec2-user/java-json.jar
export CLASSPATH
But it didn't help.
I have no errors in my code and the jar is not broken since it worked before. What should I do to fix this problem?
Specify the jar file on the classpath like this:
java -cp .:/home/ec2-user/java-json.jar main.Server
If your classpath is indeed correctly set in your .bash_profile, you could also do:
java -cp .:$CLASSPATH main.Server

Java RMI ClassNotFoundException on stub occurs in Linux but not Windows

I'm getting the following exception when attempting to use Naming.lookup() to create an RMI object:
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: project.server.data.RmiMainObjImpl_Stub
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:101)
... 2 more
Caused by: java.lang.ClassNotFoundException: project.server.data.RmiMainObjImpl_Stub
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.rmi.server.LoaderHandler$Loader.loadClass(LoaderHandler.java:1206)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at sun.rmi.server.LoaderHandler.loadClassForName(LoaderHandler.java:1219)
at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:452)
at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:185)
at java.rmi.server.RMIClassLoader$2.loadClass(RMIClassLoader.java:637)
at java.rmi.server.RMIClassLoader.loadClass(RMIClassLoader.java:264)
at sun.rmi.server.MarshalInputStream.resolveClass(MarshalInputStream.java:214)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1612)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
... 4 more
Curiously, this error only happens under Linux and AIX, but never under Windows. The error happens consistently, but seemingly works under some versions of Java:
The original jar was built with 1.6.0u30 on a Windows machine
The original jar will not work under Linux using 1.7.0u60, but will work with 1.6.0u24
The original jar will not work under AIX using 1.6 64-bit, but will work with 32-bit
Building the jar on a Linux machine using 1.7.0u60 will actually not work with the same runtime on that machine
My policy file seems to be set up correctly and is being recognized (though, I'm guessing I'd have a different error if it was not):
grant {
permission java.security.AllPermission "", "";
};
I'm executing java with a command line similar to this:
java -cp ./.:./JProjApi.jar:./MyRMI.jar -Djava.security.policy=./policy.all com/project/rmi/Main
And the code looks like this:
System.setSecurityManager(new RMISecurityManager());
rmiObj = (project.server.data.RmiMainObj_1_0) Naming.lookup("rmi://172.17.44.45/RMIMain");
I do not have access to the server-side (and I'm consuming a jar that has the necessary client-side interfaces as well).
Any ideas on what might be going wrong?
the stub class [is] not in the client-side jar.
The stub class must be in the client JAR file, unless you're using the codebase feature.
why does the Windows version work?
If it's working on one platform and not others, the stub class is clearly in the CLASSPATH on the platforms where it works, and not in the platforms where it doesn't. Or, if you're using the codebase feature, which you haven't said anything about, the codebase URL isn't accessible from all the platforms.
Figuring this out became a much lower priority, but it came back.
Network scanning showed nothing unusual.
It turned out that it did, in fact, have at least something to do with the Java version or new settings on newer Java versions.
This:
-Djava.rmi.server.useCodebaseOnly=false
Had to be added to the command line and it now works for those machines it didn't previously work on.

SciTE: ClassNotFoundException in Java when using a newly added library (but not in command prompt)

I'm currently working with the Tidy class from the JTidy library provided by SourceForge (http://jtidy.sourceforge.net/).
I have already added the .jar library to the CLASSPATH, and I can compile the Java program in SciTE without any error. But when I try to run the program, SciTE prints out this output:
>java -cp . SourceViewer3
Exception in thread "main" java.lang.NoClassDefFoundError: org/w3c/tidy/Tidy
at SourceViewer3.main(SourceViewer3.java:65)
Caused by: java.lang.ClassNotFoundException: org.w3c.tidy.Tidy
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 1 more
>Exit code: 1
Note: line 65 is where I call the constructor of the Tidy class,
Tidy tidy = new Tidy();
But when I try to run the program in command prompt, it runs smoothly without any problem. How can I fix the problem in SciTE?
Additional information: I'm using Windows 7 64 bit, SciTE 3.0.3 and Java version "1.7.0_02".
Nevermind, it is solved now.
Turns out that I must run it by using the command "java (java class name)" rather than using the default command "java -cp . (java class name)" used by SciTE, so that it doesn't override the setting of the CLASSPATH environment variable.
Sorry for the bother ^^;;

Categories