I've found several instructions on how to import user-built .class and .jar files to JPype, but I seem to be having a lot of trouble getting anything working at all.
What works: I can import standard java stuff and print HELLO WORLD and such.
Some of what I've tried:
I've tried adding -Djava.class.path with the path to a jar containing the relevant class files, to a directory structure containing (several folders down) the relevant .class files, as well as '-Djava.ext.dirs'. I've recompiled and re-installed with a different JVM location. The class I am attempting to instantiate is Outer, public, and has a public constructor.
I'm using Python 2.6.1 on OSX 10.6.
My current test file:
from jpype import *
startJVM(getDefaultJVMPath(), '-Djava.class.path=/Users/gestalt/Documents/msmexplorer_git/msmexplorer/MSMExplorer/build/classes')
java.lang.System.out.println("hello world")
msmexplorer = JPackage('org.joofee.meh.msmexplorer')
T = msmexplorer.MSMExplorer()
shutdownJVM()
If I use JClass I always get ClassNotFound exceptions from JPype; if I use JPackage I get Package not callable errors. Basically, JPype can't find my stuff.
Thanks so much!
EDIT (possibly helpful debugging stuff...):
Is there a straightforward way to print which third party java classes are available/imported?
Package not callable errors are referenced in this link) it would seem you need to make sure the java class file is accessible from the working directory. I am not sure how the jvm classpath comes into play, I would have thought how you did it would work.
You could also try loading the org package and then getting to the other packages through that one as the link I shared shows:
msmexplorer = JPackage('org').joofee.meh.msmexplorer
T = msmexplorer.MSMExplorer()
Related
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?
I have been searching the web trying to find the answer to my question, but everywhere I look seems to have too complex of a solution for a beginner like me. I have been working on this project, and just now realized that I should've made a package, or something like that. The thing is though, my program was working fine until I started dabbling with it, and now it won't work at all. I am getting this error:
Exception in thread "main" java.lang.NoClassDefFoundError: BubbleSort. class
Caused by: java.lang.ClassNotFoundException: BubbleSort.class
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:672)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at ``java.lang.ClassLoader.loadClass(ClassLoader.java:638)
Could not find the main class: BubbleSort.class. Program will exit.
Here's how my "path" looks, if I am not mistaken. I am connected to my school's Z: drive through a remote connection, and from there I have a folder called myFirstname_Lastname_A4,
which then leads me to another folder called sortingzz which I believe is supposed to have only my source files, but it also ended up with my class files in there whenever I compiled. So here's what I am doing to compile.
cd myFirstname_Lastname_A4/sortingzz
javac *.java (Works fine, this is where I end up with my Class files inside of my sortingzz folder)
java * (This is where I get the error)
I am pretty sure I am just trying to run the program wrong. Before I started messing around with stuff I wasn't ready for, I used to just run the file with my main function in it, like this
javac SortingImplementation.java
java SortingImplementation
And that for the most part worked fine, but I started having trouble calling certain classes from other classes, so thats when I found out I was suppose to do the packaging and importing stuff.
In case that is the issue, I have on the top line of every source file:
package sortingzz;
and I am importing like this:
import sortingzz.*;
This is correct, right?
UPDATE:
I decided to give up on class pathing and trying to package everything, because as usual, I am getting responses that are way over my head, and to be honest I don't think it is necessary.
After removing package and importing from everything, and once again compiling using javac *., it actually compiles this time. However whenever I try to run my class with the main in it, SortingImplementation, it tells me that
Could not find the main class: SortingImplementation. Program will exit.
I don't get it, I am looking at the SortingImplementation.class right now, with all the other classes and java files, so I am not sure what it's trying to do.
javac *.java is fine. This will compile your files. However, you only need to run the file with your main method in it: java MainClass
You say that you are using packages to organize the classes. In this case you need to set the class path using the -cp flag.
javac -cp /path/to/parent/of/package classname.java
and
java classname
Also, your main class should be declared public and should have a main()
NoClassDefFoundError occures when a class was recognised in compile time but was not available during runtime.
So the JVM can't find your class in the classpath.
using -cp flag to specify where your package is should work.
the commanc javac *.java compiles all found java files to corresponding .class files. If you all your classfiles are in the same folder, which they should, you just run your regular java SortingImplementation command.
java * would, a bit depending on your OS, yield in an undesired command. For instance, on Linux it would be expanded by the OS to java SortingImplementation.java SortingImplementation.class BubbleSort. The last one is a directory, which ofcourse is not an executable class.
I'll try to illustrate the problem as simple as I can.
I have a JAR file, which I extracted using Winrar. (The jar file contains an open source android library).
I want to modify this JAR file by adding a new class to the library.
So here are my steps:
First, I created a class using Eclipse and set the package name same as the android's library package name.
Second, I copied this java File to the folder of the other java files in the library.
Third, I tried to compile the JAVA file via the CMD using javac.
The path of the new java file and the other .JAVA and .CLASS files of the library is: C:\com\example\core\
The name of the new java file would be: "MyNewClass.java"
The command I run via the CMD is: javac C:\com\example\core\MyNewClass.java
But, during the compilation I get many errors saying: Cannot find symbols.
I've been looking up for a solution of this problem but couldn't figure how to solve it and make the new JAR File having another class that I created seperately.
What am I missing?
As per earlier comments:
Rather than trying to modify the JAR, you can get access to the full source code of the Universal Image Loader library by cloning the repository using git or hitting "Download ZIP" on the righthand side of the page you linked.
Once you have the source, import the library in your IDE. From there on you'll be able to build the whole thing from scratch, make any adjustments/modifications you like, etc.
Your classpath might be wrong or there might be some mistake in package name.
When a Java program is being compiled the compiler it creates a list of all the identifiers in use. If it can't find what an identifier refers to it cannot complete the compilation. This is what the cannot find symbol error message is saying, it doesn't have enough information to piece together what the Java code wants to execute.
Try:
javac -cp com/* C:\com\example\core\MyNewClass.java
That should make the compiler aware of all the other classes under com/...
I'm working on creating my own implementation of one of the system Java packages but am having some problems with the wrong class getting picked up when trying to use the package.
For example, lets say my package is: a.b.c.DoStuff and there is an existing Java package with the exact same name, a.b.c.DoStuff.
Using the following code in a test application, I can tell that the system class is still getting used (located in /usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar) instead of my own:
ClassLoader loader = test.class.getClassLoader();
System.out.println(loader.getResource("a.b.c.DoStuff.class");
My package has been compiled into a jar file (package.jar), and I have:
Imported the class in my test file (import a.b.c.DoStuff;)
Added package.jar to my classpath (with both "export CLASSPATH..." and using "java -classpath...")
There must be something I'm overlooking? Any thoughts on how to get my package picked up instead of the system package?
Thanks,
Chris
You can't replace classes from the standard packages unless you put your jar in the special 'endorsed' directory. And some you can't replace at all that way.
Thanks, bmargulies for the tip on the bootclasspath! Prepending my .jar file to the bootclasspath solved my problem. To summarize, I was able to use my own Java package implementation by prepending my package's .jar to the bootclasspath:
java -Xbootclasspath/p:<path-to-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.*
>>