简体   繁体   中英

Java Netbeans Classpath issue

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.

在此处输入图片说明

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,

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.

(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);
        }
    }
}

(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM