The rmi program when run asks a command.
If I gave ipconfig it works,but does not works for ping www.google.com and powercfg/batteryreport etc. I think it does not works if their is a space or slash between the input command.
Any help would be appreciated
si.java
import java.rmi.*;
import java.net.*;
import java.io.*;
public interface si extends Remote{
public String calc(String com) throws RemoteException,IOException;
}
imp.java
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import java.net.*;
import java.io.*;
public class imp extends UnicastRemoteObject implements si{
imp() throws RemoteException,IOException {
super();
}
public String calc(String com) throws IOException{
String inputLine;
Runtime r=Runtime.getRuntime();
Process p=r.exec(com);
BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
while((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
//pingResult += inputLine;
}
return inputLine;
}
}
server.java
import java.rmi.*;
import java.rmi.registry.*;
public class server{
public static void main(String args[]){
try{
si stub=new imp();
Naming.rebind("rmi://localhost:5000/example",stub);
} catch(Exception e) {
}
}
}
client.java
import java.rmi.*;
import java.util.*;
public class client{
public static void main(String args[]){
try{
si a=(si)Naming.lookup("rmi://localhost:5000/example");
Scanner s=new Scanner(System.in);
System.out.println("enter command");
String com=s.next();
System.out.println(a.calc(com));
} catch(Exception e){
}
}
}
Scanner
by default parses whitespace-delimited tokens, so you are getting and sending to the server only the first part of the input line. The server can only execute what you send it.
Use Scanner.nextLine()
. Or if you only want lines, you don't need Scanner
; use BufferedReader
and readLine()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.