Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

terminating one main class thru another mani class?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how is it possible to terminate one main class with another main class? is it possible to introduce static access in class(needs to be terminated) by which the other class culd gain access of this static access.

culd anybody there come out with a best example of this scenario?

thanks.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

If you're starting a main() method from the command line to fire up the program and then starting ANOTHER main() method from ANOTHER command window to shut it down then you have two JVMs in two processes. You'll need some kind of inter-process communication to do this.

I have an example like this in a web server:

The choice of how to send a message from Shutdown to SSWiki is simple because SSWiki is a web server. Shutdown sends an HTTP POST command to Wiki that tells SSWiki to shut down.

Does that sound at all like your problem? If so, look at Sun's All About Sockets which will show how to send simple messages from one program to another. If not, fill us in on the problem more. What are your two main() methods?
 
frank furt
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mr.James.

Here is the code that clears more. Pls, correct me.


---------------------------- class Test.java ---------------------------------

// Class Test.java needs to be terminated by another class called 'Terminator.java'.

package com.input.controls;

public class Test {
private static boolean isRunning=false;

public Test() {
super();
}

public static synchronized boolean isRunning() {
return Test.isRunning;
}

public static synchronized void setRunning(boolean isRunning) {
Test.isRunning = isRunning;
System.out.println("SETS THE RUNNING STATUS TO ====== "+isRunning);
}

public synchronized boolean getIsRunning() {
return Test.isRunning;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Inside main of Test.");
System.out.println("isRunning = " + Test.isRunning());
Test.setRunning(true);
System.out.println("isRunning = " + Test.isRunning());
while(Test.isRunning()){
System.out.println("polling....");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("OUT of while loop. isRunning = "+Test.isRunning());

}

}







---------------------------- class Terminator.java ---------------------------------

// This class needs to terminate the Test.java after checking that it is running.
// Trying to gain access thru 'static'


package com.input.controls;


public class Terminator {


public Terminator() {
super();
}

public static void main(String[] args) {
System.out.println("Terminator steps in...");
//Test.setRunning(true);
System.out.println("Status of Start Program : isRunning = "+Test.isRunning());
if(Test.isRunning()){
System.out.println("Its Running...So, i'm Shutting it Down.");
Test.setRunning(false);
System.out.println("Status of Start Program >>> "+Test.isRunning());
}else{
System.out.println("Oops...Its already dead.");
}
}

}



Running these two main classes using Eclipse IDE.

Any other approaches to terminate one main class thru another java main class would also be appreciated.

Thanks again.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you start Test and then start Terminator you have two separate JVMs and processes. They can't see each other's objects or static variables and such. You'd have to make them communicate through some inter-process technology like sockets.

If you have Test and Terminator running as two threads in the same process they can share a lot more information. See if this makes sense. Have you played with Runnable and Thread before? See Thread.interrupt() too.
 
frank furt
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi james, this is perfect all happens from a main class. but the Terminator what i have mentioned will be invoked by external program(could be of shell script since i'm running it on unix box) at the end of day. The Trigger to stop the main class comes from outside the main class.

your help was very much appreciated. thanks.

awaiting your turn.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think everything you need is in prior posts. Pick some kind of inter-process communication. There are many choices, but plain old sockets are probably easiest for a simple message like "stop".
 
You've gotta fight it! Don't give in! Read this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic