-2

I want to recognize the class type from a string given through the command line. For example, args[0] = "Integer",

Now I do in this way:

Class<?> cls = Class.forName(args[0]);

But I get "java.lang.ClassNotFoundException: Integer"

I have read that I have to use the fully-qualified name of a class in the forName(), so how can I get the string "java.lang.Integer" from the string "Integer", or "java.util.ArrayList" from "ArrayList", etc?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 3
    Well, names like `Integer` and `ArrayList` are inherently ambiguous. How is it supposed to know that you mean `java.lang.Integer` and not `some.other.coolpackage.Integer`? – Sweeper Nov 27 '22 at 16:03
  • So it's not possible? what if I want to get the fully-qualified name of a class I have in my package? (assuming you don't know the path name) – Matteo Rigat Nov 27 '22 at 16:05
  • Well I didn't say that. If you can think of some *rules* (which is what I was asking in my comment) that distinguishes the classes you actually want to find from any other class that might exist, then you can implement those rules. – Sweeper Nov 27 '22 at 16:08
  • "what if I want to get the fully-qualified name of a class I have in my package?" I'm not sure what you mean by that. If you know it is "in your package", then you know which package it is in, so you can compute the FQN, right? Do you have a `Class` object? If you do, just do `getName()`. – Sweeper Nov 27 '22 at 16:14
  • I have this exercise... "write a class whose main gets class name from the command line and prints all the methods that can be called on an instance of the passed class", I have to assume I have the whole class name? – Matteo Rigat Nov 27 '22 at 16:35
  • Yeah, I think it is reasonable to assume the input to be the FQN of a class. – Sweeper Nov 27 '22 at 16:41

1 Answers1

0

In Java, String, Integer or even BufferedInputStream are meaningless words defined by programmers. The way Java runs, it needs a fully qualified name in order to be able to distinguish java.lang.String from, for example, kotlin.String (a different language that also runs on the JVM).

This is why java.lang.String is not only a name, but also points the JVM to a package and more importantly, this packages are represented in the file structure.

This is why using class names is invalid syntax in Java unless the class is in the same package (in which case, Java can find it using your current package declaration) or if there is an import at the top (which will provide Java a fully qualified name to refer to)

In short, unless you have a set of rules where you can dynamically provide the fully qualified name, based on the short name, there's not much you could do.

Here's a short demo of a tiny script that will loop through a couple of common packages to tell you where common Java classes are:


public static void getFullyQualifiedClassName(String className) {
        String[] commonPackages = {"java.lang", "java.util", "java.io", "java.math", "java.nio", "java.net"};
        String qualifiedName;
        for (String packageName : commonPackages) {
            try {
                Class.forName(packageName + "." + className).getName();
                System.out.println("A class with that name was found at: " + packageName);
            } catch (ClassNotFoundException e) {
                System.out.println("The class is not in package " + packageName);
            }
        }

Given the input Integer, the following output occurs:

A class with that name was found at: java.lang.Integer
The class is not in package java.util
The class is not in package java.io
The class is not in package java.math
The class is not in package java.nio
The class is not in package java.net

This is probably not useful as you still need to know the possible packages in advance, but maybe it can point you in the right direction

Ieris19
  • 43
  • 5