0

I am using Netbeans 7.1.2, I am trying to run my Java application, in which the Main class tries to call another Main class from different project,

public class Main {
    public static void main(String[] args) {
        com.XXXX.XXXX.main.Main.main(new String [] {

When I tried to set the classpath in netbeans, I couldn't find libraries option in project properties.

enter image description here

There is also no library folder in my project. So now how can I set classpath to access the main class of the other project.

Thanks in advance,

  • You need to build the second project as `jar` and add that you your main project as `library` then you can call the `main` method from the `jar`. – madteapot Aug 14 '14 at 12:35
  • The jar file of the second project is already in the dependency of main project. If I cntrl+click on com.XXXX.XXXX.main.Main.main it clearly opens the main method of the other project, but still java.io.FileNotFoundException exception occurs when I try to run it. – Sathish Kannan Aug 14 '14 at 12:44
  • I just noticed that it is working in debugging mode, but not in run mode.. Don't know how. – Sathish Kannan Aug 15 '14 at 09:31

1 Answers1

1

The way you are trying to call main method from different class is not correct and I guess that is the reason it is not working. Other thing is that your question is not very clear and from your code it looks as if you are trying to call the same class's main method.

But as far as i understand you have two projects and you are trying to call second project's main method from first projects main method.

  • First step is to build your second project as jar file. Then close this project and forget about it.

  • Second step is to develop your first project and add your second project's jar as library to this project. once this is done it's just simple coding.

Below are the code snippets to achieve the functionality.

Main method of Second project (The one that is going to be library)

public class second {

    public static void main(String[] args) {
        System.out.println("This statement comes from the main method in the jar .");
        System.out.println("total params passed are: " + args.length);
        for (String string : args) {
            System.out.println("param is: " + string);
        }
    }
}

Main method of the first project (the one which will call the library's main method)

public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {

    System.out.println("This statement is from main method in the program.");


    /**
    * This is the class name. and it needs to be correct.
    * You do not need to mention project name or library name. 
    * newpackage is a package in my library and second is a class name in that package
    */
    final Class _class = Class.forName("newpackage.second");

    //here we mention which method we want to call   
    final Method main = _class.getMethod("main", String[].class);

    //this are just parameters if you want to pass any
    final String[] params = {"one", "two", "three"};

    try {
        //and finally invoke the method
        main.invoke(null, (Object) params);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
        Logger.getLogger(JavaApplication2.class.getName()).log(Level.SEVERE, null, ex);
    }

Below is what my project structure looks like after adding the library project

madteapot
  • 2,208
  • 2
  • 19
  • 32