I have written the following SNMP client code to display the hardware information
-
import java.io.IOException;
-
import org.snmp4j.CommunityTarget;
-
import org.snmp4j.PDU;
-
import org.snmp4j.Snmp;
-
import org.snmp4j.Target;
-
import org.snmp4j.TransportMapping;
-
import org.snmp4j.event.ResponseEvent;
-
import org.snmp4j.mp.SnmpConstants;
-
import org.snmp4j.smi.Address;
-
import org.snmp4j.smi.GenericAddress;
-
import org.snmp4j.smi.OID;
-
import org.snmp4j.smi.OctetString;
-
import org.snmp4j.smi.VariableBinding;
-
import org.snmp4j.transport.DefaultUdpTransportMapping;
-
public class SNMPManager
-
{
-
Snmp snmp=null;
-
String address=null;
-
public SNMPManager(String add)
-
{
-
address=add;
-
}
-
public static void main(String[] args)throws IOException
-
{
-
SNMPManager client=new SNMPManager("udp:127.0.0.1/161");
-
client.start();
-
String sysDescr=client.getAsString(new OID(".1.3.6.1.2.1.1.1.0"));
-
System.out.println(sysDescr);
-
}
-
private void start()throws IOException
-
{
-
TransportMapping transport=new DefaultUdpTransportMapping();
-
snmp = new Snmp(transport);
-
transport.listen();
-
}
-
public String getAsString(OID oid)throws IOException
-
{
-
ResponseEvent event=get(new OID[]{oid});
-
return event.getResponse().get(0).getVariable().toString();
-
}
-
public ResponseEvent get(OID oids[])throws IOException
-
{
-
PDU pdu=new PDU();
-
for(OID oid:oids)
-
{
-
pdu.add(new VariableBinding(oid));
-
}
-
pdu.setType(PDU.GET);
-
ResponseEvent event=snmp.send(pdu, getTarget(),null);
-
if(event != null)
-
{
-
return event;
-
}
-
throw new RuntimeException("GET timed out");
-
}
-
private Target getTarget()
-
{
-
Address targetAddress=GenericAddress.parse(address);
-
CommunityTarget target=new CommunityTarget();
-
target.setCommunity(new OctetString("public"));
-
target.setAddress(targetAddress);
-
target.setRetries(2);
-
target.setTimeout(1500);
-
target.setVersion(SnmpConstants.version2c);
-
return target;
-
}
-
}
-
When I run this program, that shows the exception as given below:
Exception in thread "main" java.lang.NoClassDefFoundError: org/snmp4j/TransportMapping
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.ja va:2442)
at java.lang.Class.getMethod0(Class.java:2685)
at java.lang.Class.getMethod(Class.java:1620)
at sun.launcher.LauncherHelper.getMainMethod(Launcher Helper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.snmp4j.TransportMapping
at java.net.URLClassLoader$1.run(URLClassLoader.java: 366)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:4 23)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 56)
... 6 more
Please help me to correct this error. Thanks in advance...
Kara Hewett
Did you add the jar SNMP4J to WEB-INF/lib?