Sunday, 5 November 2017

Java program to turn on a PC from remote location

Is this possible to turn on a remote PC using a Java program ? Of course it is possible.
As we know that remote PC can be turned on. But it require some tools/softwares and configuration in both PCs.

Let see what is required for this ?

Note:- I have tested it on Windows XP, Windows 7 professional and Windows 10 edu. machine as a target (client) and Ubuntu 16.04 and Ubuntu 14.04 as source (server).

To turn on a PC from remote, we require it to configure to Wake on LAN (WOL). So, how to configure Wake on LAN, please read this post.

After configuring target PC, You also require some tools on source PC. So, for required tools and information on source machine, please read this post.

Now, after preparing both machine, you require obviously  Java on source machine to run a Java program. Its version should be 7 or higher. It may also run on lower Java version, but I have not tested on lower versions.

Here is the Java code :-

import java.io.*;

String mac="MAC address of traget machine";
String com="wakeonlan";
String command=com+" " +mac;

 try {
        Process process = Runtime.getRuntime().exec(command);
        System.out.println("the output stream is "+process.getOutputStream());
        BufferedReader reader=new BufferedReader( new                         InputStreamReader(process.getInputStream()));
        String s; 
        while ((s = reader.readLine()) != null){
            System.out.println("The inout stream is " + s);
        }                   
    } 
catch (IOException e) {
        e.printStackTrace();
    }

If everything will be configured properly, you turn on remote PC using above Java code.

No comments:

Post a Comment

Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...