java Swing Application start only one time, how to restrict ?
hi
I have to trouble to start application only once, how to restrict , I have to open different command prompt and run my application the first command prompt run successfully , and I want the second command prompt run the same application , this will not run. this will be give an error message , how to design this arch. for swing application . any one code with explain me
Thanks and regards
SASIKUMAR
[429 byte] By [
SASIMSITa] at [2007-11-27 4:56:58]

> hi
> I have to trouble to start application only once, how
> to restrict , I have to open different command prompt
> and run my application the first command prompt run
> successfully , and I want the second command prompt
> run the same application , this will not run. this
> will be give an error message , how to design this
> arch. for swing application . any one code with
> explain me
>
> Thanks and regards
> SASIKUMAR
I assume by this you wish to say you want to write a Java Application (regardless of whether it uses swing or not) that will prevent itself from running concurrently, right? In other words, only one can run at one time.
Well, bind a specific ServerSocket, that can only be done once, and exit if that fails.
I.E. When the application starts, the first thing it should do is to open a ServerSocket on a specific port, and never accept connections (unless, of course, you need network functionality anyway). Since the OS itself, will only allow this once for a specific port, any subsequent attempts to open the ServerSocket will fail.
So, like I said, open a ServerSocket, and if it fails exit.
Hi plz do check following code
package myClasses;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class JFrameDemo extends JFrame {
public static boolean isWindowOpen = false;
public static void main(String[] args) {
JFrameDemo demo = new JFrameDemo();
demo.setBounds(400, 600, 200, 300);
//demo.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
File pro = new File("C:\\Great\\fundoo\\MyProject\\JavaSource\\myClasses\\myProperties.properties");
FileInputStream in;
try {
in = new FileInputStream(pro);
Properties properties = new Properties();
properties.load(in);
String str = (String) properties.getProperty("isWindowOpen");
if("true".equals(str)) {
FileOutputStream out = new FileOutputStream(pro);
properties.setProperty("isWindowOpen", "false");
properties.store(out, "isWindowOpen");
out.close();
demo.show(demo);
} else {
throw new UnsupportedOperationException();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
demo.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try{
File pro = new File("C:\\Great\\fundoo\\MyProject\\JavaSource\\myClasses\\myProperties.properties");
FileOutputStream out = new FileOutputStream(pro);
Properties properties = new Properties();
properties.setProperty("isWindowOpen", "true");
properties.store(out, "isWindowOpen");
out.close();
System.exit(0);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
});
}
public void show(JFrame demo1){
if(!isWindowOpen) {
demo1.show();
} else {
throw new UnsupportedOperationException();
}
}
}
is this is fine for you?