aspose file tools*
The moose likes Beginning Java and the fly likes Working with multiple main() methods and static variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Working with multiple main() methods and static variables" Watch "Working with multiple main() methods and static variables" New topic
Author

Working with multiple main() methods and static variables

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hi. I have an application that brings up a main menu. Initially when this
main menu comes up, it goes out to our users DB and determines what type of user they are (i.e : costingUser, orderEntry,sysAdmin, etc.).
I have made these boolean static variables in the main menu app.
On this main menu are different buttons that launch different modules, such as a costing module, order entry module, etc.
Each of these modules has it's own main method. So I figured since I've already determined what type of user it is in the main menu app, I don't need to do this again in each of the modules when they are launced. However, this doesn't work. It doesn't recognize my static variables at all. Is this because when I launched the other modules, they were launched using their own main method. That means they are running in a separate JVM, right?
So how can I get the users security from the main app, and pass it along to the other modules so I don't have to build security into each module?
Thanks for any help. (Sorry if this is confusing!!)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Depends what you mean by "launched." If you mean you used Runtime.exec(), then yes, they're in separate processes. Maybe you could pass the authentication information as command-line arguments when you start the other processes.
If by launch you just mean explicitly calling some other main(), then the new applications are running in the same JVM, and should be able to share static variables; this is then probably just a programming error, and you could show us some of the relevant code.


[Jess in Action][AskingGoodQuestions]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8907
    
    8

When you say you "launch" a program, are you using Runtime.exec("java SomeClass") or calling SomeClass.main(stringArray)? The first will create a new vm and your static variables will be inaccessable (or uninitalized). The second will simply invoke the static method main() on SomeClass. Your static variables should be accessible in the second.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Sorry about that. I am using Runtime.exec() to run my programs.
If I were to just call the main() method of these other classes, I would
have to change my code to not call System.exit(0) when they are closed out,
correct? Otherwise it will close everything won't it?
In your opinion, which of these two methods is the best (most efficient) way of accomplishing what I want to do? Using Runtime.exec(), or calling
the main method ?
Thanks for your input !
Just a quick note, I just tried calling the main() method, and it acted like it was working, but the app I called never showed up. Is there something special I need to do?
All I did was: JavaProgram jp = new JavaProgram();
Thanks again!
[ November 14, 2003: Message edited by: Jennifer Sohl ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8907
    
    8

Let's see if Ernest and I can get identical answers to this question!
Originally posted by Jennifer Sohl:
Sorry about that. I am using Runtime.exec() to run my programs.
If I were to just call the main() method of these other classes, I would
have to change my code to not call System.exit(0) when they are closed out,
correct? Otherwise it will close everything won't it?

You are correct.

In your opinion, which of these two methods is the best (most efficient)

Creating a new VM creates a new heavyweight process, starts all manner of threads (GUI event handler, GC, main and so on) and creates a new heap. Invoking the static main() method uses the existing one. I'd say the latter is more efficient.

Just a quick note, I just tried calling the main() method, and it acted like it was working, but the app I called never showed up. Is there something special I need to do?
All I did was: JavaProgram jp = new JavaProgram();

That's the only line in your main() method? The constructor of JavaProgram invokes the JFrame.show() method (or whatever brings the gui to the fore)? Sounds like it should work. If it doesn't, show us some code.
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hey, I'm back!! I just tried invoking my main method as follows:
com.storekraft.ord.JavaProgram.main(new String[] {})
and it worked.
I think I am going to go the route of not using Runtime.exec().
Now, I have another question. I "launched" my application by invoking the main method.
Now, on the application that I invoked the main method on, I have opened up another JFrame (Order Inquiry) from it (no main method). Since I changed my code to just do a dispose() on the invoked app, rather than a
System.exit(0), it doesn't close the inquiry that I opened up. How do I get my program to close any opened windows associated with the invoked main app when it is disposed of?
Thanks again!!
Does that make sense?
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Just wanted to let you know, I talked to my supervisor, and it's not a big deal that the windows associated with the main app don't close when it is disposed of. One less thing for me to worry about.
Also wanted to say THANK YOU !! for all of your help.
I really, really appreciate it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Working with multiple main() methods and static variables