I know this question is not new but I am posting this after going through enough googling around.
I have a jar file which I don't control. I mean I have not written the Java class in there and all I know is that there is a class named "hist" in it which takes two arrays and gives out the output. Now I have to use PHP to call this class "hist" in the jar file named "histvol.jar". I have installed the PHP/Java Bridge and it is installed correctly.
But I don't understand how to call this jar file in PHP and where to place this jar file.
This is what I did:
Installed tomcat and php-javabridge
Placed the jarfile "histvol.jar" under Tomcat/webapps/JavaBridgeTemplate621/webinf/lib/
Went to xampp/htdocs and created a file named testjava.php
<?php
require_once("http://localhost:8080/JavaBridgeTemplate621/java/Java.inc");
$System = java("java.lang.System");
$myclass=java("histvol");
echo $System->getProperties();
?>
Class not found exception is expected because I am not calling it anywhere in PHP, but how do I call it?
I am lost, please help (I don't know a word of Java).
Ok I got it finally, I just had to do this:-
$myclass=new java("histvolone.histvol");
instead of
$myclass=new java("histvol");
It worked !
Related
In this previous question, I was trying to rework some Matlab code and figure out a package called javaplex to be compatible with Octave; it uses Java, but is tooled for Matlab, hence that issue. Now in an interval of time, I was busy/running simulations, and hadn't gotten around to a final step - actually using the package, with most all of the difficulties worked out. It turns out that another step exists: I need to convert an Octave array to a Java array (although I'm not sure why this issue didn't come up in Matlab).
To do so, I have turned to this script, in which the comments indicate that when using it, it
Assumes the JIDT [Java Information Dynamics Toolkit] jar is already on the java classpath - you will get a java classpath error if this is not the case.
So I go to the JIDT GitHub page and download this package. Now I am not a very avid user of java, so I believe I am failing to see something fairly straightforward: I am not sure where the "JIDT jar" is that is referenced in the above block quote! I can't find such a particular jar file to put in Octave's java classpath. In this tutorial for JIDT, they say you need the "infodynamics.jar" file in the classpath (page 9). I'm not sure what jar file I should be looking for, and where. Any help understanding the nature, name and location of this jar file (within the infodynamics toolkit folder) would be appreciated!
As an inevitable follow-up question, because this will come up upon resolving this issue, I would like to clarify the following procedure is how to add a jar file to the Octave (static) java classpath (following this answer here, I wasn't sure if I was implementing correctly):
I create a file called "javaclasspath.txt" inside of the directory I use in Octave.
I enter the name of files as follows: "./path/to/your-file.jar"
I suppose my main issue here is where do I start the path (all the way back with "C:/..."?), and do I put this "javaclasspath.txt" file in the directory folder I will be using most of the time in Octave?
Edit: I cannot find "infodynamics.jar" as shown here:
The JIDT jar is named infodynamics.jar and it is located in the root of the downloads infodynamics-dist-1.4.zip file.
Ok, I'm stumped here. I'm using Matlab version 2013b with a Java RTE of 1.7.0_11 and I'm trying to run a simple piece of code to see if Matlab is able to read the .jar file and nothing seems to be working.
Here is the Java code, which is compiled to a .jar named JavaOCT.jar, which is placed in the Matlab working directory:
package VTK;
public class vtkVolumeView{
public int Test(){
return 10;
}
}
That is it, no other dependencies, nothing fancy. In Matlab, I try:
javaaddpath('\JavaOCT.jar'); %<-Directory and name are 100% correct
import VTK.*; %<-Package name from above
methodsview VTK.vtkVolumeView; %<-Can't find the class, argh!
Matlab kicks back that it can't find the class.
Things I've done to try and solve the problem:
Reverted to the exact same JDK as the Matlab RTE
Tried an older 1.6 JDK
Done lots of stack overflow research to try and solve it 1 2 3 4
Tried used javaclasspath and pointing to the compiled class instead
Read the Matlab documentation 5
Using clear -java after the javaaddpath
Any help would be appreciated, it is driving me nuts!
Update: Daniel R suggested just javaaddpath('JavaOCT.jar') which doesn't work either.
Final update: It finally works! I wasn't building the .jar properly. In IntelliJ, click on the project and hit F4. This brings up the Project Structure, then go to Artifacts and click the green + button and add DirectoryContent and then point to the out\production. Once this is done, as mentioned by others, it should show up in Matlab as an expandable .jar.
I don't know which operating system you are using, but the ./ seems invalid.
Try javaaddpath('JavaOCT.jar'); or javaaddpath(fullfile(pwd,'JavaOCT.jar'));.
What does exist(fullfile(pwd,'JavaOCT.jar')) return?
Some things to try:
Add the class file. When using a package, you need to add the class file in at the host of the package. For example, if your code is here:
\\full\path\to\code\VTK\vtkVolumeView.class
Then use:
javaaddpath('\\full\path\to\code')
I'm still suspicious of your *.jar path. You should usually use absolute paths when adding jar files. Try adding the results of which('JavaOCT.jar')
How did you make your jar file? Does it contain the appropriate directory structure implied by your package declaration?
To the best of my knowledge, Class.getResourceAsStream() gets an InputStream directed at a file within the .jar file? If I'm wrong about this, please correct me.
Is there any way to temporarily 'load' an external file into the .jar, so that the getResourceAsStream() method can retrieve it?
For example loading C:/folder/file.txt into the .jar so that MyClass.class
.getResourceAsStream("file.txt") would return a working InputStream to the file, or at least a copy of it that has been 'loaded' temporarily into the .jar.
I'm sorry I've worded this so badly, I hope you can understand what I'm trying to do, wasn't quite sure how to explain it simply.
Nope, the getResourceAsStream() method gets a file from the classpath. That includes anything that is specified like this on the command line (windows version, linux uses ':' as delimiter):
java -cp "path;path2;path3;path4/some.jar" your.main.Clazz
I'm going to be honest and say that I have never deployed an applet, but it seems to be incredibly painful. The <applet code> doesn't work on chrome so im using the jnlp to hopefully get past this nightmare, but it only seems to add to my migrane. Everything runs fine in eclipse, but when it comes to deployment.. I might be needing a new computer if I can't figure this out haha.
Started out creating the java jar (jar cvf WebGame.jar bin):
Then I created the JNLP file that looks like this (named bounce.jnlp):
I thought I was starting to get somewhere when I created my html file (Bounce.html):
So I decided to upload everything to test out how it works on chrome:
It looked like my game was loading on the browser but I ended up getting this:
So after messing around with it for a couple of hours I'm all like this:
If anyone could help me out I would DEFINITELY appreciate it, (I can't really afford to buy a new computer at the moment).
I suspect that your StartingPoint class is not actually in the bin package, this is the hint for me:
java.lang.NoClassDefFoundException: bin/StartingPoint (wrong name StartingPoint)
This would indicate that your class is in the default package (i.e. has no package declaration at the beginning).
Try changing your JNLP to
main-class="StartingPoint"
and create your jar file from within the bin directory:
$ cd Documents/Java/TheBasics/WebGame/bin
$ jar cvf ../WebGame.jar *
I have read the documentation and several websites on exactly how to do this, however Matlab does not seem to pick up the classes that I have added to the dynamic java class path. Nor do I use the right syntax to correctly construct the object.
I have an class HandDB and which to create an object of this type and invoke it's static methods to connect to a SQL database. The class has an empty constructor and takes no parameters. The class is part of a package 'nuffielddb' which I made in a project within Netbeans. All the files are on my usb stick which is my E:\ drive...
I would like to be able to use all the classes within the package. The package is contained at E:\nuffielddb.
I entered the following commands into Matlab:
javaaddpath('E:\');
javaclasspath; % Output from java class path includes E:\ within dynamic path
str = java.lang.String('Test'); % Works fine
db = nuffieldbd.HandDB(); % Does not work - undefined variable or class error
Interesting I typed 'import nuffielddb.*;' and received no error.
Just where am I going wrong?
Thanks for your help btw!
Ah problem solved! Well not solved in a sense! I found out it's actually a problem with my matlab installation and I have no idea how to fix it :-(
Never mind, it works on the computers at the office :-)
if your classes are in a .jar file, make sure your classpath includes the .jar file name itself (not just the directory it's in).
Also if the MATLAB JRE is Java 1.5 (R2006b is, whereas R2009a is Java 1.6, not sure when they switched), make sure your classes are compiled with 1.5 as a target, not 1.6, otherwise MATLAB will not be able to use them.
Minor note: .* imports will never error, so they're not diagnostic. They simply add a package to the list that Matlab searches through when trying to resolve a class name. Nonexistent packages are ignored.
>> import this.package.does.not.exist.*
>>