How to call a java package Packages.java.net.InetAddress.getByName("www.google.com")

droid
Kilo Explorer

I am trying to call  Packages.java.net.InetAddress.getByName("www.google.com") in "Scripts - Background"

But this gives me an error -  

Script -

var i = Packages.java.net.InetAddress.getByName("www.google.com");

gs.print(i);

Result -

Attempted script access to inaccessible member denied - java.net.InetAddress:getByName:(Ljava/lang/String;)Ljava/net/InetAddress;
Evaluator: java.lang.SecurityException: Illegal access to method getByName(string) in class java.net.InetAddress       Caused by error in script at line 1   ==>     1: var i = Packages.java.net.InetAddress.getByName("www.google.com");             2: gs.print(i);
Background message, type:error, message: Illegal access to method getByName(string) in class java.net.InetAddress

Although a few other packages like URL executes fine -

Script -

var i = new Packages.java.net.URL("http://www.google.com");

gs.print(i.getHost());

Result -

*** Script: www.google.com

Why would InetAdress not work and other java.net classes work?

10 REPLIES 10

Inactive_Us1474
Giga Guru

Actually all the packages call are being moved to GlideScriptable Objects. Mostly this is for glide packages.


http://wiki.servicenow.com/index.php?title=Packages_Call_Replacement_Script_Objects#gsc.tab=0



You can check what all classes are accessible using just


gs.print(Packages.java.net.URL); >> result would be Java Class


somehow this getByName is not found..



Hope it helps,


Akhil


Actually,



The class is InetAddress


gs.print(Packages.java.net.InetAddress);


returns [JavaClass java.net.InetAddress]


If you use gs.print(Packages.java.net.InetAddress.getByName()); // Inet Address is present but not any method



All these Package calls are going to move to Script API's to provide better functionality.



----------------


Hope it helps,


Akhil



I think i am doing something wrong here.


Packages.java.net.InetAddress.getByName() is actually a function present in the package, but somehow we are not able to use(call) it.


Look at the result of this script which prints all functions available in a class -


var obj = Packages.java.net.InetAddress;


var methods = [];


for (var m in obj) {


      if (typeof obj[m] == "function") {


              methods.push(m);


      }


}


gs.print(methods.join(","));


Result-


[0:00:00.009] Script completed in scope global: script



Attempted script access to inaccessible member denied - java.net.InetAddress:getLoopbackAddress:()Ljava/net/InetAddress;
*** Script: getAllByName,getByName,getLoopbackAddress,getByAddress,getLocalHost

This proves that there is actually all the functions as mentioned in the documentation of InetAddress