简体   繁体   English

Java Netbeans Classpath问题

[英]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, 我正在使用Netbeans 7.1.2,正在尝试运行Java应用程序,其中Main类尝试从其他项目中调用另一个Main类,

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. 当我尝试在netbeans中设置类路径时,在项目属性中找不到库选项。

在此处输入图片说明

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. 您尝试从其他类调用main方法的方式不正确,我想这就是它不起作用的原因。 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. 另一件事是您的问题不是很清楚,并且从您的代码看来,您似乎在尝试调用同一类的main方法。

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. 但是据我了解,您有两个项目,并且您正尝试从第一个项目的main方法调用第二个项目的main方法。

  • First step is to build your second project as jar file. 第一步是将第二个项目构建为jar文件。 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. 第二步是开发您的第一个项目,并将第二个项目的jar添加为该项目的库。 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 下面是添加库项目后我的项目结构的样子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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