1

I am trying to create a method that will instantiate a class based on a given interface. At the moment I am trying to instantiate a class based on a class name but I keep getting ClassNotFoundException.

Can anyone tell me what I am doing wrong?

public class Message implements IExample{
    @Override
    public String showMessage() {
        return "merge";
    }
}

public static void main(String[] args) throws Exception{
    Object mess = Class.forName("Message").newInstance();
}

EDIT

I have tried :

Object mess = Class.forName("com.MyExample.Message").newInstance();
Object mess = Class.forName("Project.MyExample.Message").newInstance();
Object mess = Class.forName("MyExample.Message").newInstance();

They all throw ClassNotFoundException and a window which tells me "Source Not Found" with a button (Edit Source Lookup Path..) that let's me browse documents.

Both the main class and Message classes are in a project called "Project" and a package called MyExample

Ryan Ransford
  • 3,224
  • 28
  • 35
aleczandru
  • 5,319
  • 15
  • 62
  • 112
  • Are you sure class `Message` is on the class path? Also, you need the fully qualified class name; `com.whatever.Message` – raffian Aug 27 '13 at 15:41
  • what is your package? where does your `Message.class` lives? – user902383 Aug 27 '13 at 15:57
  • Are the class Message and the main method inside a common class ? if so, give a look here: http://stackoverflow.com/questions/2097982/is-it-possible-to-create-an-instance-of-nested-class-using-java-reflection – Emanuele Ivaldi Aug 27 '13 at 16:07

4 Answers4

4

You need to specify the fully qualified class name to Class.forName(String).

Parameters: className the fully qualified name of the desired class.

If Message is in package com.package, that would be com.package.Message.

Object mess = Class.forName("com.package.Message").newInstance();

That class must be on the classpath when launching the application.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
2

If your Message class is in a package, you need to specify the full name, such as edu.myschool.mypackage.Message.

Eric Stein
  • 13,209
  • 3
  • 37
  • 52
0

You need to provide the fully qualified name of the Class. If Messageis in package x.y then you have to provide x.y.Message.

Object mess = Class.forName("x.y.Message").newInstance();
phlogratos
  • 13,234
  • 1
  • 32
  • 37
0

The class name shouldn't include the ProjectName, just the package name structure that is defined in your src folder.

If you are using Eclipse, make sure you defined the src folder correctly in your project. Here is how you can change the src folder for your app (if needed).

the Structure should be something like this:

Project
 |
 +-src
 | |
 | +-com
 |   |  
 |   +-MyExample
 |     |
 |     +-MyClass
 +-build

In this case, you should be able to say

Class.forName("com.MyExample.MyClass").newInstance();

It's very important how your structure in the src folder is defined and make sure you have the right src folder defined in your IDE project settings.

nucatus
  • 2,196
  • 2
  • 21
  • 18