all groups > vj# > march 2005 >
You're in the

vj#

group:

J# Multiple main method problem


J# Multiple main method problem MattMcSpirit
3/8/2005 9:09:09 AM
vj#:
Hi there,

Im pretty new to Java and J#, however, ive produced 2 classes, found below,
that simulate the changing of traffic lights. The first, traffic_light,
prints a line in a console every second, each time changing the colour of the
traffic light. The second class, strangely_cycling_traffic_light, inherits
more or less everything from traffic_light, yet changes the sequence of
lights.

Here is the code:

/**
* traffic_light program.
*/

public class traffic_light implements Runnable {
final String name;
final String[] sequence;

/**
* Construct a default traffic light.
* @param name The name of the light.
*/
public traffic_light (String name) {
this(name, new String[]{
"Stop",
"Prepare to go",
"Go",
"Prepare to stop",
});
}

protected traffic_light (String name, String[] sequence) {
this.name = name;
this.sequence = sequence;
}

/**
* Run Method for changing states
*/
public void run () {
while (true) {
for (int index = 0; index < sequence.length; index++) {
final String state = sequence[index];

printState(state);
timer(1000);
}
}
} // End run.

/**
* Delays for a period.
* @param delay The delay period.
*/

public static void timer (int delay) {
try {
Thread.sleep(delay); //timer sleeps for 1 second
} catch (InterruptedException exception) {
//Does nothing
}
} // End timer.

/**
* Prints out a state
* @param state The state to print.
*/

public void printState (String state) {
System.out.println( "The " + name + " traffic lights tell the driver to
" + state);
}

/**
* Main method to run the program
* @param args Command line arguments.
*/

public static void main (String args[]) {
traffic_light trafficLight = new traffic_light("normal");
trafficLight.run();

} // End main.
} // End TrafficLight

And here is the code for the other class:

public class strangely_cycling_traffic_light extends traffic_light {

/**
* Construct a strangely cycling traffic light.
* @param name The name of the light.
*/

public strangely_cycling_traffic_light (String name) {
super(name, new String[]{
"Stop",
"Prepare to stop",
"Go",
"Prepare to go",
});
}

public static void main (String args[]) {
strangely_cycling_traffic_light trafficLight = new
strangely_cycling_traffic_light("strangely cycling");
trafficLight.run();
}
}

Both of these files work perfectly in a Java program such as JCreator, but
the project requires a submission in the form of a Visual Studio Project,
meaning the files are stored together in the one project, and here is where
the problem lies....

Each of the files has a main method, and whichever is set in Visual Studio
to be the start-up object will run first, meaing the other cannot be
demonstrated.

Is there any way of just compiling and running the 2 files individually? Or
making some kind of button interface (i have no experience in this) where the
user, upon building the project, is presented with a choice of 2 buttons, one
for normal traffic light and one for the strange one? Would that even work,
bearing in mind the 2 main methods?

Would there be any way to adjust the code to remove the need for the 2 main
methods? I need to have 2 separate classes, so they cant be condensed into
one.

Any help would be great.

Thanks,

RE: J# Multiple main method problem Avid J# Programmer
3/10/2005 11:29:11 AM
Well, writing a GUI for this shouldn't be too hard, so going with the two
button tehcnique would certainly work. But if you want to keep it simple and
keep it in a console window, i'd suggest making a 3rd class, where the user
decides which trafficlight they want to see (you could even do it through the
arguments even). your main method inside the 3rd class could look somthing
like this:

public static void main (String args[])
{
//prompt the user for input
System.out.println ("Please choose a traffic light: (normal, strange)");
java.io.BufferedReader consoleTextIn new java.io.BufferedReader (new
java.io.InputStreamReader (System.in));
//read input from the console window
String input = onsoleTextIn.readLine ();
if (input.equalsIgnoreCase ("normal"))//run normal traffic light
{
traffic_light trafficLight = new traffic_light("normal");
trafficLight.run();
}
else//run strange traffic light
{
strangely_cycling_traffic_light trafficLight = new
strangely_cycling_traffic_light("strangely cycling");
trafficLight.run();
}
}

so like i said, stuck this into a 3rd class (call it anything you want) and
set it to run on startup. This way the user can decide which traffic light
they want to see.

[quoted text, click to view]
RE: J# Multiple main method problem MattMcSpirit
3/10/2005 12:49:05 PM
Thanks for this, it is really helpful.

Il let you know if i run into any problems.

Thanks,

Matt

[quoted text, click to view]
RE: J# Multiple main method problem MattMcSpirit
3/10/2005 1:01:09 PM
Hi again,

Im getting an error message : unreported exception java.io.IOException; must
be caught or declared to be thrown.

My code for the 3rd class is as follows:

public class Selector {

public static void main (String args[])
{
//prompt the user for input

System.out.println ("Please choose a traffic light: (normal, strange)");

java.io.BufferedReader consoleTextIn = new java.io.BufferedReader (new
java.io.InputStreamReader (System.in));
//read input from the console window

String input = consoleTextIn.readLine ();

if (input.equalsIgnoreCase ("normal"))//run normal traffic light
{
traffic_light trafficLight = new traffic_light("normal");
trafficLight.run();
}
else//run strange traffic light
{
strangely_cycling_traffic_light trafficLight = new
strangely_cycling_traffic_light("strangely cycling");
trafficLight.run();
}
}

} // End Selector

The error is pointing to this line: String input = consoleTextIn.readLine
(); but im not sure what to do, do i use a catch statement?

Thanks,

Matt

[quoted text, click to view]
RE: J# Multiple main method problem Avid J# Programmer
3/10/2005 1:11:10 PM
oh yea, sorry about that. This isn't a hard one to solve
Yes, try and catch the IOException around the readLine call. It would look
somthing like this

String input;
try {
input = consoleTextIn.readLine ();
}catch (java.io.IOException e){System.out.println ("Unable to get input from
the console");}
if (input == null)return;//got bad input

That should fix it. Make sure you declare the input String before the try
statement, so it stays alive outside the try/catch blocks. you also want to
check if the input variable stays null, in which case you would have to exit
the program.
[quoted text, click to view]
Re: J# Multiple main method problem George Birbilis [MVP J#] [9880]
3/27/2005 8:56:27 PM
in fact I think VS.net/J# doesn't allow one to have more than one "main"
methods in their project, which could be considered a "bug" since most other
Java IDEs I know allow you to do so and you select which class has the main
method you want to use for running your project (what is the executable file
that is)

can anyone check and confirm there's no such option? If so you can file it
as a bug at:
http://msdn.microsoft.com/productfeedback

[quoted text, click to view]

Re: J# Multiple main method problem George Birbilis [MVP J#] [9880]
3/30/2005 12:50:27 AM
asked about it (didn't have even the little time needed to check it
unfortunately) and got the following useful info:

J# does allow more than 1 main method. One can choose the right main
method by specifying /main switch in compiler (also settable through
VS). If the switch is not specified, the compiler picks the first one
it gets and gives a warning (which may be treated as error if warn as
error is true).

[quoted text, click to view]

AddThis Social Bookmark Button