14

I have a project "A" and I need to call a class "c" from another project "B".

I have done the following. Click in "A" -->Properties -->Build Path --> and in one tab of Java Source I selected the project B. --> Accept

Now I can create objets of Class "c" but when I run the project I get "ClassNotFoundException"

Update,

I keep getting java.lang.ClassNotFoundException.

In "JSF" -->Properties -->Build Path --> Projects ---> I Added the Project:

enter image description here

In Run --> Run Configurations --> ClassPath I had:

enter image description here

and now I have added the project "JIRA" and "JIRA dependencies" enter image description here

JIRA project has this dependencies:

enter image description here

And I get the following error:

enter image description here

but this class is in M2_REPO:

enter image description here

SOLUTION

I add "JIRA dependencies" only in JARs without Maven: enter image description here and now it´s run.

  • I think the jars need to be copied in WEB-INF/lib folder, but I don't know how to do it with Maven. You can try to do it manually to check if that solves your problem... – Baldrick Aug 03 '12 at 20:12
  • @Baldrick I was able to resolve the error following your directions. I update with solution – Pablo Gomez Sanmartin Aug 04 '12 at 12:54

7 Answers7

14

You should add another project in "Project" tab, or add class folder of the project in "Libraries" tab.

example

Rangi Lin
  • 9,303
  • 6
  • 45
  • 71
  • I did this but when I used statement import static class_name_from_project_B and getting error "The import can't be resolved" in class_name_from_project_A – vikramvi May 22 '17 at 14:29
  • Found the problem; I didn't specify any package for class in the project which I had included under Projects. Found this issue through https://stackoverflow.com/questions/2335211/what-is-the-default-package-in-which-my-classes-are-put-if-i-dont-specify-it – vikramvi May 22 '17 at 15:39
11

You need to add project B to the Run configuration used by project A. In the menu Run -> Run configurations... , add the project B in the tab 'classpath' of your run configuration.

Baldrick
  • 23,882
  • 6
  • 74
  • 79
7

Put the Project B on the Build path, then do a Clean project from Project Menu option... and then use it..

-

Click in "A" -->Properties -->Build Path --> Projects ---> Add the Project --->Ok
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • can you please clarify how to import a class from Project B statically in a class of Project A ? I used statement import static class_name_from_project_B and getting error "The import can't be resolved" – vikramvi May 22 '17 at 14:26
  • Found the problem; I didn't specify any package for class in the project which I had included under Projects. Found this issue through https://stackoverflow.com/questions/2335211/what-is-the-default-package-in-which-my-classes-are-put-if-i-dont-specify-it – vikramvi May 22 '17 at 15:40
2

In order to call methods from Classes in Java, you first need the instance of the class.

So in your code you need to do something like the following:

  1. acquiring instance:

    MyClass instanceOfMyClass = MyClass.getInstance();

  2. calling the method:

    instanceOfMyClass.someFunction(...);

This means that when you call getInstance() it will return instance of MyClass if such an instance already exists, or it will create a new one, if there's no instance. After acquiring an instance of a class, you simply call it's methods, if they have the appropriate access modifiers.

As per java doc java.lang.classNotFoundException comes in following cases:

  1. When we try to load a class by using MyClass.someMethod() method and MyClass is not available in classpath.
  2. When Classloader try to load a class by using findSystemClass() method.
  3. While using loadClass() method of class ClassLoader in Java.

To Resolve the above exception we need to:

  1. First find out the jar file on which problematic class file is present for example in case of "com.mysql.jdbc.driver" its mysql-connector-java.jar. If you don't know how to find which jar file a particular class you can simply do "Ctrl+T" in Eclipse and type the name of class, It will list all the jar in the order they appear in eclipse classpath.

  2. Check whether your classpath contains that jar, if your classpath doesn't contain the jar then just add that class in your classpath.

  3. If it’s present in your classpath then there is high chance that your classpath is getting overridden or application is using classpath specified in jar file or start-up script and to fix that you need to find the exact classpath used by your application.

Next Door Engineer
  • 2,818
  • 4
  • 20
  • 33
  • and if MyClass has no method getInstance()? – ice Aug 03 '12 at 09:30
  • @ice In the Java librairies there are many classes that can be built either by using the constructor method (Foo a = new Foo()) or by calling a static getInstance() method (Foo a = Foo.getInstance()). The Java API comes with has a method called getInstance(). – Next Door Engineer Aug 03 '12 at 09:41
  • And you can use = String s = String.getInstance()? The problem in this Question "Foo a = new Foo()" throws a ClassNotFoundException – ice Aug 03 '12 at 09:52
  • @ice: I added some notes on handling the exception in the answer! Hope it helps. – Next Door Engineer Aug 03 '12 at 10:08
0

If B is in the same workspace and alos open you have to put int in "Projects" not is "Source". If you want to make A "more" independent, you have to export B.jar and the add it to "Libraries"

ice
  • 309
  • 1
  • 7
0

Also consider to use maven. Its very useful if you are having multi-module projects. Just add the dependencies of abother project and maven will take care of rest..!

Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82
0

create project "A" as jar and add this jar in your project "B" build path.then you can call which class you want and import that class package name with calss name.

madhu
  • 1,083
  • 3
  • 12
  • 31