0

I have a java project containing two Main classes.

These two Main classes are conceived to be run one at a time, so that every time I have to set Eclipserunning configuration to change the class to be loaded.

Is there a way to create a very simple JFrame or whatever UI that allows the user to programmatically choose which class to load and then run the related Main class when the application is delivered?

Roberto
  • 243
  • 1
  • 5
  • 15
  • you can copy/duplicate your running configuration and edit main class in one of them to use the 2nd one. you can start any of them from Run list – Yazan Sep 29 '16 at 09:50

3 Answers3

1

When an application is delivered to your customer; then shouldn't ask the user to manually select a "Main" class to run with.

Instead: you create one application that your customer is using; and that applications provides different functionalities (if it makes conceptual sense to combine those functions; because they are somehow similar).

Otherwise, if you are really talking about two very different "applications"; then simply spoken: they should not be in the same project; or "deployment unit" at least.

Edit: given your comment; I would suggest: don't think about different mains; think about one main and the ability to load different configurations then.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Yes, I agree with you. The problem is that I'm using an Eclipse based platform which automatically creates the project and also allows the creation of different Main classes. These Main classes share the same package's classes and only differ in some settings like the Thread handling. These are conceived as different experiments that can be run. This is why I'd like to know if there is a way to choose which Main class to run from a simple user interface. – Roberto Sep 29 '16 at 10:12
  • Updated my answer. Maybe that helps. – GhostCat Sep 29 '16 at 10:50
1

It looks like you want to let the user choose which class to run.

  • Within the same JVM, you can invoke() the main Method like they show here.

  • For separate JVMs, you can use ProcessBuilder like they show here.

Community
  • 1
  • 1
Catalina Island
  • 7,027
  • 2
  • 23
  • 42
0

You can use two differenz start configurations in eclipse for the same project but with different Main classes.

Niklas P
  • 3,427
  • 2
  • 15
  • 19
  • But I need a `UI` to allow the user to choose which `Main` class to launch from a compiled version. – Roberto Sep 29 '16 at 09:51
  • You can use a parameter which has to be defined with the start of java and parse this in the main method (`args[0]`) or you can define your own `JFrame` with two `JButtons` and an `ActionListener` that handles these two Buttons and works like the main method when parsing the param from the console. – Niklas P Sep 29 '16 at 10:42