This week's giveaway is in the Threads forum.
We're giving away four copies of Java Concurrency Live Lessons and have Doug Schmidt on-line!
See this thread for details.
Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I am getting null value insted of system information.  RSS feed

 
Yatin Shah
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sir,

I want to get Server System Information on client machine. I am getting null value. I am sending my code here with. Please check it and tell me What's the problem with this code?

(1) Interface:

//CountRMI.java

public interface CountRMI extends java.rmi.Remote {
public int getSum() throws java.rmi.RemoteException;
public void setSum(int val) throws java.rmi.RemoteException;
public int increment() throws java.rmi.RemoteException;
public String getProperties() throws java.rmi.RemoteException;
}

(2) Implementation:

// CountRMIImpl.java

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.awt.*;
import java.lang.*;

public class CountRMIImpl extends UnicastRemoteObject implements CountRMI
{
private int sum;
private String finalString,name,ver,user,lang,reg,cpu,jname,vmname,VmVen,VmVer,venurl;

public CountRMIImpl(String name) throws RemoteException
{

super();

try
{
Naming.rebind(name, this);
sum = 0;
finalString = ("\n\n\nSystem :\n "+name+"\n Version "+ver+"\n\n"+"Registered To :\n User "+user+"\n Language "+lang+"\n Region "+reg+"\n\n"+"Computer :\n "+cpu+"\n\n"+"Java Information :\n "+jname +"\n "+vmname+"\n "+VmVen+"\n Version "+VmVer+"\n "+venurl);
}

catch (Exception e)
{
System.out.println("Exception "+e.getMessage());
e.printStackTrace();
}
}

public int getSum() throws RemoteException
{
return sum;
}

public void setSum(int val) throws RemoteException
{
sum = val;
}

public int increment() throws RemoteException
{
return sum++;
}

public String getProperties() throws RemoteException
{
return finalString;
}

}

(3) Client Class:

//CountRMIClient.java

import java.io.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.lang.*;
import java.net.*;

public class CountRMIClient
{
public static void main(String [] args)
{
String[] s = new String[25];
String[] str = new String[25];
FileReader fr;
FileWriter fw;
PrintWriter pw;
BufferedReader br;
int lineNo=0,noLines;

try
{
fr = new FileReader("IPList.txt");
br = new BufferedReader(fr);

while((s[lineNo]=br.readLine()) != null)
lineNo++;

br.close();
fr.close();
}

catch(FileNotFoundException fnfe)
{
System.out.println("File not found.... try another");
}

catch(IOException ioe)
{
System.out.println("I/0 Exception while reading file");
}

while(true)
{
noLines = 0;

for( int j = 0; j < lineNo; j++)
{
try
{
CountRMI myCount = (CountRMI)Naming.lookup("rmi://"+s[j]+"/mycount");

str[noLines] = s[j];
noLines++;

// Set Sum to intial value of 0.

System.out.println("Setting Sum to 0");
myCount.setSum(0);

// Calculate Start time.

long startTime = System.currentTimeMillis();

// Increment 1000 times

System.out.println("Incrementing");

for(int i = 0; i < 1000; i++)
{
myCount.increment();
}

// Calaculate stop time; print out statistics.

long stopTime = System.currentTimeMillis();

System.out.println("Average Ping = "+ ((stopTime - startTime)/1000f) +" micro-second");
System.out.println("Sum = "+myCount.getSum());
System.out.println("Server "+s[j]+" is working");
System.out.println(myCount.getProperties());
System.out.println();
}

catch(RemoteException re)
{
System.err.println("Server "+s[j]+" is not ready.");
System.out.println();

}

catch(NotBoundException nb)
{
System.err.println(" File Not Found "+nb);
}

catch(MalformedURLException mfue)
{
System.err.println(" Proble with URl Change and try again....");
}
}

try
{
// Creating File for Working Server.

fw = new FileWriter("OnList.txt");
pw = new PrintWriter(fw);

for(int k = 0; k < noLines; k++)
pw.println(str[k]);

fw.close();
pw.close();
}

catch(IOException ie)
{
System.err.println(" File Not Found "+ie);
}

try
{
System.out.println("Wait Few Seconds..........");
System.out.println();
Thread.sleep(18000);
}

catch(InterruptedException ie)
{
System.out.println("Interrupted the sleep...");
}
}
}
}

(4) Server Class:

// CountRMIServer.java

import java.rmi.*;
import java.rmi.server.*;
import java.awt.*;
import java.lang.*;

public class CountRMIServer
{
public static void main(String [] args)
{
//Create and Install the security manager

try
{
CountRMIImpl myCount = new CountRMIImpl("mycount");
System.out.println("CountRMI Server ready.");

String name = System.getProperty("os.name");
String ver = System.getProperty("os.version");
String user = System.getProperty("user.name");
String lang = System.getProperty("user.language");
String reg = System.getProperty("user.region");
String cpu = System.getProperty("sun.cpu.isalist");
String jname = System.getProperty("java.runtime.name");
String vmname = System.getProperty("java.vm.name");
String VmVen = System.getProperty("java.vm.vendor");
String VmVer = System.getProperty("java.vm.version");
String jclass = System.getProperty("java.class.version");
String venurl = System.getProperty("java.vendor.url");

}

catch(Exception e)
{
System.out.println("Exception: "+e.getMessage());
e.printStackTrace();
}
}
}

Please, check my code and replay me.

Thanking You
Yatin
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem looks like your CountRMIServer and CountRMIImpl classes aren't finished. CountRMIServer gets all the properties using System.getProperties(), but it never passes these values into the CountRMIImpl class. CountRMIImpl doesn't even have any methods to set these values - it just has class level attributes, which are never assigned a value (which explains why you are getting null. You'll either need to pass all the property values into the CountRMIImpl constructor, or you'll need to get all the values using System.getProperties() inside the CountRMIImpl constructor rather than in the CountRMIServer.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!