calling a class in another project Eclipse

前端 未结 7 901
执笔经年
执笔经年 2020-12-03 14:16

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

相关标签:
7条回答
  • 2020-12-03 15:18

    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.

    0 讨论(0)
提交回复
热议问题