I'm currently making a Java program that loads classes from a different package. It allows users to delete .class files from a package and prevent a crash. I therefore want to load all these classes using:
Class.forName("this.is.a.package.RandomClass");
The only problem is that when I try that it says that the class is not found. If I put the class in the same package it works, but if I wanted to load it from the package "this.is.a" Class.forName("this.is.a.package.RandomClass"), it doesn't work. Any help appreciated!
I know I should probably be using URLClassLoaders but I don't know how I would use them to load a file inside a zipped jar.
Related
I want to load classes that are inside a jar, that is located within another jar. Following is a hierarchy tree illustrating the location of the .class.
A.jar
|---A.class
|---B.jar
|---InterfaceA.class
So I have a class 'A'. This class implements an interface, 'Interface B' which is inside B.jar. A third class named 'C' loads at runtime an instance of A.class. However, I end up with a java.lang.ClassNotFoundException. Any idea on how to fix this issue? I've seen plenty of posts that explains how to dynamically load classes from jar files, however I haven't seen anything that explains how to load classes from a jar file that is within another jar file.
Thanks for your time
I understand that there are many similar questions and answers, but none of the answers fit what I am trying to do. I have a file called gui.java and I am trying to turn it into a jar file, LifeGame.jar I keep the .java and the .class in a folder, and when I try to archive it, it works, but when I try to run the file, it gives me Could not find or load main class gui. I do give a manifest called "META-INF:MANIFEST.MF" and the folder is stored on my desktop.The manifest looks like:
Main-Class: gui
I would like some advice on what to do and how to fix this problem. (As I have already said, I understand this is a clone of many other questions, but the examples I've seen don't work for me in my situation)
EDIT:
Some details on my scenario:
I have multiple classes in gui.java but only one of them is public, the rest aren't private of public. It is on my desktop and when I try java gui it gives the same error as when I try archiving it into a Jar.
Name of your main class should be gui. check your .class file which is created to be gui.class or any other. It doesn't matter the name of .java file but the name of .class file.
Lets say we want to package our Java application into a jar file (including the dependencies). The dependencies are copied into one separate directory (lets say libs/). I have read that are two approaches to make the java launcher find these classes:
1) Implement a custom class loader code (loader/launcher pattern) that will load necessary classes before the main code of the application is executed (it is described here: http://vladimirvivien.wordpress.com/2011/10/07/86/)
2) Add the libs/ directory to the "Class-Path:" header in meta-inf/manifest.fm
Which solution is better and why?
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()
I'm looking to create a universal logging file for all of my companies Java applications. I've done something similiar in C++, where you simply import the file.
My question is, what is the most effective/efficent way to create a public Java file (basically what do you create it as, and then how do you reference it)? I'm assuming its by importing your own class or jar? Thanks!
It's not really clear what you mean, but it sounds like you're trying to create a library to be used by multiple applications.
I'd suggest that the other projects should simply refer to it as a jar file - whether they build that jar file from source or fetch one from some company-wide repository which is updated when the logging code changes is up to you. From the application code, you'd just import it as any other class - whether the class is found in a jar file or as a .java file is irrelevant in the consuming Java source code.
It's worth noting that there are already many other logging APIs for Java - I would think very carefully before you create a new one.
In Java you don't import the file you import the class. The class that is imported is either in a compiled .class file or packaged in a .jar file. By convention, package your logger utilities in a .jar and distribute that.
You import the class by name regardless if it's yet to be compiled from a source file or already packaged in a .jar.