How to call a java package Packages.java.net.InetAddress.getByName("www.google.com")
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2017 11:20 PM
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?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2017 01:51 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2017 02:12 AM
Yes, this function does exist within the Java packages, but the sandboxing of the platform will prevent you from accessing core Java package code. There may be some exceptions. As such, this function you want is not permitted by the platform.
I think we need to get an idea of what requirement you are trying to achieve. Perhaps there is an alternative method to achieve that requirement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2017 02:25 AM
Hi Pranay
The platform is "sandboxed" to prevent any Java package calls, which is evident from the error you have provided. It has been like this since the Fuji release. You are only permitted to execute documented Java code that has been made GlideScriptable, where many are essentially wrappers to the Java Foundation Library.
Believe it or not, this has been done for your benefit. I have seen in the past (going way back) where Java package calls were used and, following an upgrade, they stopped working, resulting in weird behaviour or destroying functionality. Particularly worse when the JDK is updated - Classes and their methods can change.
You will find available calls documented in the Developer Portal: https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&id=server.
Also, I would err on the side of caution when referring to the Wiki. This is relevant to Fuji and older releases. The platform has gone through many changes since that time and some server-side GlideScriptable APIs/methods may have been dropped/changed. All this is kept up to date in the Developer portal or the Documentation site.
Regards
Shahid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2017 12:51 AM
I've been hit up a few times for the usage of the term "sandboxing". To be honest, I could not think of a better term to use, hence why I put it into quotes.
Essentially, the platform will not allow you to access all Java classes and methods. Fine, some may work as desired. But, just because you can access one method of a class doesn't mean you can access other methods of the same class.
Yes, there's talk of white listing Package calls (combination of classes and methods). Still, I will stick to my guns and say try your best not to. When you arrive to this point in an implementation, just ask yourself this question over and over again: "Is there really no other (or better) way to achieve the requirement?"
Don't get too technical on a certain pathway to achieve the requirement(s), think of different ways to achieve. That requirement is what your client needs, not what you want to code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2017 12:58 AM
Thanks for the suggestion.
I am using MID Server to do the same thing now.
It would have been good if we could have done it from the front end using javascript (theoretically faster).
Anyways, let's just let the user wait for the result to come from MID Server and do the household chores in the meantime ☺