9

I've been running different individual Java .java files in the Netbeans IDE by right-clicking the .java files themselves in the Project Explorer of Netbeans (the portion normally at the upper left part of Netbeans).

However, i've been googling on how to make a class file run another class file using code, but to no avail.

I have a project named "loadanotherfile" with 2 files, namely: Loadanotherfile.java and otherfile.java

I'm trying to make Loadanotherfile.java run otherfile.java, but I'm not exactly sure how. I read about Classloaders and URLClassloaders however these methods don't seem suitable for my purpose of running another .java file.

Below is the code of the 2 files i mentioned.

Loadanotherfile.java

package loadanotherfile;

public class Loadanotherfile {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Hello World!");
        // TODO code application logic here
    }
}

otherfile.java

package loadanotherfile;

public class otherfile {

    public static void main(String args[])
    {
        System.out.println("This is the other file.");
    }
}

I have a feeling that the task has something to do with using the "import" syntax (namely something like import loadanotherfile.* but even if my guess is correct, I'm still not sure on how to make my Loadanotherfile.java run otherfile.java using code.

How can I load otherfile.java using Loadanothefile.java?

Cheers

1
  • 1
    The purpose of running other class file is not clear. To load a class in memory you can use Class.forName("className"); method by specifying the name of the class as a parameter. Commented Nov 1, 2012 at 9:42

4 Answers 4

12

In Loadanotherfile.java

otherfile.main(args);
Sign up to request clarification or add additional context in comments.

Comments

7

Compile the two together, and then from Loadanotherfile,

otherfile.main(args);

will do the trick. You don't need to import since you're in the same package. Note the linked tutorial.

I would investigate (however) class instantiation, and creating an instance of a new class to invoke upon. Invoking static methods from static methods isn't very OO.

4 Comments

yes, this did the trick, also had to add a line similar to Matt Clark's answer (otherfile otherFile = new otherfile();) thanks!
@LastManStanding You would not have to add that extra line. It will work without that.
@LastManStanding, if you had to do that, then main() from otherfile is not static as you stated.
@rid Or he is mixing with the casing. He probably tried with otherFile.main(args); instead of otherfile.main(args);.
2

Try This:

className.main(Args){
}

This works! ive tested it myself.

Comments

0

Check the public void main line. If there IOException and not there then insert in Loadanotherfile.java

use this

otherfile.main(args);{
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.