I have an unusual problem which is concerned to dynamic loading java .class file at run-time. All I want to do is to load a .class file and basing on it create a Class object.
Input: an absolute path of .class file.
Basing on it i want to load class by ClassLoader, so I need a path of root directory where file is located and full class name e.g com.test.MyClass. Basing on mentioned absolute path I can only get a class name but I can't get a package name which is "hiden" in this file.
Here is code of my "loading class method":
public static void loadClass(String directory){
// Get file root directory
String rootDirectory = new File(directory).getParent();
// Get rid of file extension
String className = getFileNameWithoutExtension(directory);
URL[] urls = null;
ClassLoader cl = null;
try {
// Convert File to a URL and save them
urls = new URL[]{new File(rootDirectory).toURI().toURL()};
// Create a new class loader with the directory
cl = new URLClassLoader(urls);
// Load in the class
dynamicClass = cl.loadClass(className);
}
catch (MalformedURLException e)
{
}
catch (ClassNotFoundException e)
{
}
catch (NoClassDefFoundError e)
{
// Basing on error message get the class package name
String classPackage = getClassPackage(e.getMessage());
try {
// Load the class once more!
dynamicClass = cl.loadClass(classPackage);
}
catch (ClassNotFoundException ex)
{
}
}
}
Second method is used to get package name from exception message:
private static String getClassPackage(String errorMsg){
// Start and end index of cutting
int startIndex = errorMsg.lastIndexOf(" ") + 1;
int endIndex = errorMsg.length() - 1;
// Let's save a substring
String classPackage = errorMsg.substring(startIndex, endIndex);
// Replace char '/' to '.'
classPackage = classPackage.replace('/', '.');
return classPackage;
}
Code of method getFileNameWithoutExtension:
private static String getFileNameWithoutExtension(String path){
int start = path.lastIndexOf(File.separator) + 1;
int end = path.lastIndexOf(DOT);
end = start < end ? end : path.length();
String name = path.substring(start, end);
return name;
}
Where the static final variable is:
private static final String DOT = ".";
And here is my question: is it possible to get package name from .class file without using this kind of trick?
You can use the Foo.class.getPackage().getName() method to determine this.
public Package getPackage()
Returns:
the package of the class, or null if no package information is available from the archive or codebase.
Using getName() :
public String getName()
Returns:
The fully-qualified name of this package as defined in section 6.5.3 of The Java™ Language Specification, for example, java.lang
Since you already have the required data in className, just use it again. You don't need the getClassPackage method.
catch (NoClassDefFoundError e)
{
// Basing on error message get the class package name
//But we already have the class name in className variable!
//String classPackage = getClassPackage(e.getMessage());
try {
// Load the class once more!
dynamicClass = cl.loadClass(className);
}
catch (ClassNotFoundException ex)
{
}
}
And if you want to get the package name only (not sure why), you can just get it from the class name:
String packageName = className.substring(0, className.lastIndexOf('.'));
dynamicClass = cl.loadClass(packageName);
You can do like this-
String packName = new Object(){}.getClass().getPackage().getName();
System.out.println(packName);
Related
I try to read fields from an external jar. We name the external File: 'Data.jar'.
Data.jar contains a class called Constants with some public, static, final fields. For example:
public static final String string1 = "Test";
public static final int integer1 = 1;
How can I access the external file in order to get the value of the fields string1 and integer1? Is it possible to use reflections? I know the external jar and the structure.
Edit:
The external jars structure is an older version of my current project. So I use the URLClassLoader and call the class Constants, I will get the value of my current project, and not the jars one. So is there a way to only call the external jars classes?
Solution:
public Object getValueFromExternalJar(String className, String fieldName) throws MalformedURLException
{
Object val = null;
// calling the external jar
URLClassLoader cL = new URLClassLoader(new URL[] { jarURL }, null);
//the null is very important, if the jar is structural identical to this project
try
{
// define the class(within the package)
Class<?> clazz = cL.loadClass(className);
// defining the field by its name
Field field = clazz.getField(fieldName);
field.setAccessible(true);
// get the Target datatype
Class<?> targetType = field.getType();
Object objectValue = targetType.newInstance();
// read the value
val = field.get(objectValue);
}
catch (ClassNotFoundException | NoSuchFieldException | SecurityException | IllegalAccessException
| InstantiationException e)
{
LOGGER.log(Level.SEVERE, "An error while accessing an external jar appears", e);
}
finally
{
try
{
cL.close();
}
catch (Exception e)
{
}
}
return val;
}
Some example code using an URLClassLoader based on your description, as dan1st suggested:
URLClassLoader cl = new URLClassLoader(new URL[] {new File("Data.jar").toURI().toURL()});
Class<?> clazz = cl.loadClass("Constants");
String string1 = (String) clazz.getField("string1").get(null);
int integer1 = clazz.getField("integer1").getInt(null);
I guess you have to change the example to match your structure.
I have two java projects MASTER and PLUGIN. PLUGIN has dependencies to MASTER and its intent is to extend a class found in MASTER, called SCRIPT.
Once I have declared a SCRIPT (myScript), I want to move the .class file to a folder that MASTER can access. I want MASTER to dynamically load and instantiate that class as a SCRIPT.
I've looked for quite a bit and tried different solutions, but I always get a ClassNotFoundException exception.
I would prefer to do this without passing arguments to the JVM at startup.
Is it even possible? This is my current solution: "currentPath" is "etc/etc/myScript.class
try {
OUT.ln("initiating script " + currentPath);
File file = new File(currentPath);
File parent = file.getParentFile();
String name = file.getName().split(".class")[0];
// Convert File to a URL
URL url = parent.toURI().toURL();
URL[] urls = new URL[]{url};
// Create a new class loader with the directory
#SuppressWarnings("resource")
ClassLoader cl = new URLClassLoader(urls);
current = (SCRIPT) cl.loadClass("main.script." + name).newInstance();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Unable to load script " + currentPath);
}
if the class you want to load is defined within a package like:
main.script.myScript
and you want to load this class from a folder like c:/myclasses,
then you have to put this class to c:/myclasses/main/script/myScript.class
and then instantate the classloader with the basefolder like:
URL[] urls = new URL[]{new URL("file://c:/myclasses")};
ClassLoader cl = new URLClassLoader(urls);
then the class can be loaded by using the qualified class name:
cl.loadClass("main.script.myScript").getDeclaredConstructor().newInstance()
if you want to keep the class at a specific folder without considering the package structure you could do something like this:
public static void main(String[] args) {
try {
File file = new File("etc/etc/myScript.class");
String className = file.getName().split(".class")[0];
String packageName = "main.script.";
byte[] bytes = Files.readAllBytes(Path.of(file.getPath()));
MyClassLoader myClassLoader = new MyClassLoader(Thread.currentThread().getContextClassLoader());
Object o = myClassLoader.getClass(packageName+className, bytes).getDeclaredConstructor().newInstance();
System.out.println(o);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Unable to load script ");
}
}
public static class MyClassLoader extends ClassLoader {
public MyClassLoader(ClassLoader parent) {
super(parent);
}
public Class<?> getClass(String name, byte[] code) {
return defineClass(name, code, 0, code.length);
}
}
I am developing a class loader that will load plugins into my software. I have a jar file with two things in it, the package containing my code, and a text file containing the name of the class that I want to load from the jar. Is there a way for my app to read the text in the file and get the class name, then load the class with that name from the jar file?
This is the code that works:
content = new Scanner(new File("plugins/" + listOfFiles[i].getName().replaceAll(".jar", "") + "/" + "plugin.cfg")).useDelimiter("\\Z").next();
URL[] urls = null;
try {
File dir = new File("plugins/" + listOfFiles[i].getName());
URL url = dir.toURL();
urls = new URL[]{url};
} catch (MalformedURLException e) {
}
try {
ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass(content.replaceAll("Main-Class:", ""));
Method enable = cls.getMethod("enable", (Class<?>[]) null);
enable.invoke(enable, null);
}catch (Exception e) {
System.out.println("One of the installed plugins might have an invalid plugin.cfg.");
}
The code reads a file with the name of the main class in it, and then loads that class from the jar file it extracted earlier.
I have a form with that code:
public Form()
{
initComponents();
try
{
File file= new File("avatar.jpg");
BufferedImage image= ImageIO.read(file);
}
catch (IOException ex)
{
System.out.println("Failed to load image");
}
}
The problem is that the code always throws the IOException and enters in the catch block.
So the file isn't read.
I have created the project with Netbeans 7.2, and the directory looks like this:
What's the problem? Maybe the file shouldn't be there but in the father directory? Or what?
Is your image being packaged within your jar? to find this out, extract you jar file like you would an ordinary zip file and check if the image is anywhere there (normally located by jarname\packagename\filename. If so then you'll need to extract your image as a resource using getResourceAsStream().
It would be something like:
public class Test {
private static final String absName = "/yourpackage/yourimage.jpg";
public static void main(String[] args) {
Class c=null;
try {
c = Class.forName("yourpackage.Test");//pkg is the package name in which the resource lies
} catch (Exception ex) {
// This should not happen.
}
InputStream s = c.getResourceAsStream(absName);
// do something with it.
}
public InputStream getResourceAsStream(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader();
if (cl==null) {
return ClassLoader.getSystemResourceAsStream(name); // A system class.
}
return cl.getResourceAsStream(name);
}
public java.net.URL getResource(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader();
if (cl==null) {
return ClassLoader.getSystemResource(name); // A system class.
}
return cl.getResource(name);
}
private String resolveName(String name) {
if (name == null) {
return name;
}
if (!name.startsWith("/")) {
Class c = this;
while (c.isArray()) {
c = c.getComponentType();
}
String baseName = c.getName();
int index = baseName.lastIndexOf('.');
if (index != -1) {
name = baseName.substring(0, index).replace('.', '/') + "/" + name;
}
} else {
name = name.substring(1);
}
return name;
}
}
Reference:
Accessing Resources
It looks like you have a namespace of poker.*
It all depends on where the jvm is initialized from.
Where is your main? Is it in /Users/ramy/NetBeansProjects/Poker/src?
Also, I suggest you use getResource() for all of your file loading needs, especially inside jars.
this.getClass().getResource("/resource/buttons1.png")
or
this.getClass().getResourceAsStream("/resource/TX_Jello2.ttf")
You can find out where your programs default path is by doing the following:
System.getProperty("user.dir");
Without seeing the error I would say the most likely cause is it can't find the file. So I suggest you replace "avatar.jpg" in the File constructor with the absolute file path to it. e.g.
File file = new File("INSERT_PATH_TO_FILE/avatar.jpg");
You cannot assume the image will be "there" because the relative path between your .java and the image seems ok.
Accessing a resource depends of your "kind" of project (Web, standalone....). In your case, you can try to get the image from your classpath
final File inputFile = new ClassPathResource("....").getFile();
final BufferedImage inputImg = ImageIO.read(inputFile);
Lets say I have a java package commands which contains classes that all inherit from ICommand can I get all of those classes somehow? I'm locking for something among the lines of:
Package p = Package.getPackage("commands");
Class<ICommand>[] c = p.getAllPackagedClasses(); //not real
Is something like that possible?
Here's a basic example, assuming that classes are not JAR-packaged:
// Prepare.
String packageName = "com.example.commands";
List<Class<ICommand>> commands = new ArrayList<Class<ICommand>>();
URL root = Thread.currentThread().getContextClassLoader().getResource(packageName.replace(".", "/"));
// Filter .class files.
File[] files = new File(root.getFile()).listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".class");
}
});
// Find classes implementing ICommand.
for (File file : files) {
String className = file.getName().replaceAll(".class$", "");
Class<?> cls = Class.forName(packageName + "." + className);
if (ICommand.class.isAssignableFrom(cls)) {
commands.add((Class<ICommand>) cls);
}
}
Below is an implementation using the JSR-199 API, i.e. classes from javax.tools.*:
List<Class> commands = new ArrayList<>();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(
null, null, null);
StandardLocation location = StandardLocation.CLASS_PATH;
String packageName = "commands";
Set<JavaFileObject.Kind> kinds = new HashSet<>();
kinds.add(JavaFileObject.Kind.CLASS);
boolean recurse = false;
Iterable<JavaFileObject> list = fileManager.list(location, packageName,
kinds, recurse);
for (JavaFileObject classFile : list) {
String name = classFile.getName().replaceAll(".*/|[.]class.*","");
commands.add(Class.forName(packageName + "." + name));
}
Works for all packages and classes on the class path, packaged in jar files or without. For classes not explicitly added to the class path, i.e. those loaded by the bootstrap class loader, try setting location to PLATFORM_CLASS_PATH instead.
Here is an utility method, using Spring.
Details about the pattern can be found here
public static List<Class> listMatchingClasses(String matchPattern) throws IOException {
List<Class> classes = new LinkedList<Class>();
PathMatchingResourcePatternResolver scanner = new PathMatchingResourcePatternResolver();
Resource[] resources = scanner.getResources(matchPattern);
for (Resource resource : resources) {
Class<?> clazz = getClassFromResource(resource);
classes.add(clazz);
}
return classes;
}
public static Class getClassFromResource(Resource resource) {
try {
String resourceUri = resource.getURI().toString();
resourceUri = resourceUri.replace(esourceUri.indexOf(".class"), "").replace("/", ".");
// try printing the resourceUri before calling forName, to see if it is OK.
return Class.forName(resourceUri);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
If you do not want to use external depencies and you want to work on your IDE / on a JAR file, you can try this:
public static List<Class<?>> getClassesForPackage(final String pkgName) throws IOException, URISyntaxException {
final String pkgPath = pkgName.replace('.', '/');
final URI pkg = Objects.requireNonNull(ClassLoader.getSystemClassLoader().getResource(pkgPath)).toURI();
final ArrayList<Class<?>> allClasses = new ArrayList<Class<?>>();
Path root;
if (pkg.toString().startsWith("jar:")) {
try {
root = FileSystems.getFileSystem(pkg).getPath(pkgPath);
} catch (final FileSystemNotFoundException e) {
root = FileSystems.newFileSystem(pkg, Collections.emptyMap()).getPath(pkgPath);
}
} else {
root = Paths.get(pkg);
}
final String extension = ".class";
try (final Stream<Path> allPaths = Files.walk(root)) {
allPaths.filter(Files::isRegularFile).forEach(file -> {
try {
final String path = file.toString().replace('/', '.');
final String name = path.substring(path.indexOf(pkgName), path.length() - extension.length());
allClasses.add(Class.forName(name));
} catch (final ClassNotFoundException | StringIndexOutOfBoundsException ignored) {
}
});
}
return allClasses;
}
From: Can you find all classes in a package using reflection?
Start with public Classloader.getResources(String name). Ask the classloader for a class corresponding to each name in the package you are interested. Repeat for all classloaders of relevance.
Yes but its not the easiest thing to do. There are lots of issues with this. Not all of the classes are easy to find. Some classes could be in a: Jar, as a class file, over the network etc.
Take a look at this thread.
To make sure they were the ICommand type then you would have to use reflection to check for the inheriting class.
This would be a very useful tool we need, and JDK should provide some support.
But it's probably better done during build. You know where all your class files are and you can inspect them statically and build a graph. At runtime you can query this graph to get all subtypes. This requires more work, but I believe it really belongs to the build process.
Using Johannes Link's ClasspathSuite, I was able to do it like this:
import org.junit.extensions.cpsuite.ClassTester;
import org.junit.extensions.cpsuite.ClasspathClassesFinder;
public static List<Class<?>> getClasses(final Package pkg, final boolean includeChildPackages) {
return new ClasspathClassesFinder(new ClassTester() {
#Override public boolean searchInJars() { return true; }
#Override public boolean acceptInnerClass() { return false; }
#Override public boolean acceptClassName(String name) {
return name.startsWith(pkg.getName()) && (includeChildPackages || name.indexOf(".", pkg.getName().length()) != -1);
}
#Override public boolean acceptClass(Class<?> c) { return true; }
}, System.getProperty("java.class.path")).find();
}
The ClasspathClassesFinder looks for class files and jars in the system classpath.
In your specific case, you could modify acceptClass like this:
#Override public boolean acceptClass(Class<?> c) {
return ICommand.class.isAssignableFrom(c);
}
One thing to note: be careful what you return in acceptClassName, as the next thing ClasspathClassesFinder does is to load the class and call acceptClass. If acceptClassName always return true, you'll end up loading every class in the classpath and that may cause an OutOfMemoryError.
You could use OpenPojo and do this:
final List<PojoClass> pojoClasses = PojoClassFactory.getPojoClassesRecursively("my.package.path", null);
Then you can go over the list and perform any functionality you desire.