0

I would like to know, how to call the main method as it is in a JButton into a JFrame with an action performed, i mean, just press Compare Button & execute the code from main class (Main class is separate from JFrame code).

public static void main(String[]args){

  Comparative comp = new Comparative();


    if(comp.loadComparative(args[0])){
        comp.compareDbs();
        comp.sendEmail();
    }         

 }

private void CompareActionPerformed(java.awt.event.ActionEvent evt) {


         ?????????????

}
man
  • 35
  • 9
  • 1
    are you trying to invoke a separate application (separate JAR and in a different classloader) or just trying to perform the **same operations as the code contained in that main method**? – Alonso Dominguez Mar 05 '14 at 15:51
  • same operations as the code contained in that main method – man Mar 06 '14 at 14:22

5 Answers5

3

If the main class is on the classpath you can use reflection :

private void CompareActionPerformed(java.awt.event.ActionEvent evt) {
    MyMainClassToCall.main(myArgs);
}

If the class is located elsewhere, likely in a jar, you can certainly use an URLClassLoader to load the class which contains the main method, then use

myMainClass.getMethod("main", String[].class).invoke(null, myArgs);
Antoine Marques
  • 1,379
  • 1
  • 8
  • 16
  • i don't need to give args, it must take it from the file properties, run. – man Mar 06 '14 at 15:35
  • You can use `new String[0]` as a replacement for `myArgs`. What do you mean by `take from file properties` ? You want to load `myArgs` from file properties then call `main` or you want to call `main` with empty args, then load properties from file ? – Antoine Marques Mar 06 '14 at 16:11
  • correct no args in code, then call them from a xml file, ex. give right click to the project, properties, then run, then args & there i call the args from im going to load the file ex. "c:\comparatives\file.xml" netbeans – man Mar 06 '14 at 16:13
  • Ok, so invoke using an empty `String[]` as i told, then in you main method, load the arguments for file using `java.util.Properties` or something else =) – Antoine Marques Mar 06 '14 at 16:16
  • Did you manage to solve your problem in the end ? Please don't forget to accept the response if its the one that helped you. – Antoine Marques Mar 11 '14 at 12:42
1

You can just get the arguments you need and then call it using the name of the class where it is:

MainClass.main(args);
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
1

To give a complete answer we actually need to know what's the name of the class that contains that main method. Also, I'm struggling to understand such a weird requirement but I'll do my best to come up with a useful answer.

To invoke your main method you need to access it through the class that contains it, since it's a static method. You also need to provide an array of arguments mainly because it seems that your main method is using the first of the elements in the arguments array. So something like this would work:

private void CompareActionPerformed(java.awt.event.ActionEvent evt) {
    String[] args = new String[] { "myparam" };
    MainClass.main(args);
}

Now, said that, let me note that such an invocation of a main method is a very bad practice, you could achieve the same copying the contents of your main method into your event handler CompareActionPerformed. Or even better, creating a separate and independent class with an static method that performs the same that you need from your main method. Then invoke that new static method from your main class and from your event handler (assuming that all the code is accessible from the same class loader).

Alonso Dominguez
  • 7,750
  • 1
  • 27
  • 37
  • i know it's really weird to do it & i know that it's a bad practice but this app was like that, i didn't create it & i haven't enough time to fix it :( – man Mar 05 '14 at 16:09
  • @man then just invoke it as I stated in my example, using the right classname, of course – Alonso Dominguez Mar 06 '14 at 10:28
  • how can i call the main method without args because the args are load from the File-Properties\Run? If i set same code as u wrote it, and leave args in blank it doesn't work because it needs a parameter(arg) – man Mar 06 '14 at 15:51
0

I think it's a bad practice , you should follow some design pattern , like MVC or something , when JVM starts it looks for the "main" method and start from there , the thing you can do before calling main method is putting some code in a static braces like ... public class test { static { some code } public static void main(String[] args){ } }

this code will be executed before the main

Exorcismus
  • 2,243
  • 1
  • 35
  • 68
0

There is just one class, it is Comparative inside it's all the code incluing the main....

man
  • 35
  • 9
  • you should add that information to your original question (click on edit and modify the question) instead of adding an answer to your own question. – Alonso Dominguez Mar 06 '14 at 10:29