in Rhino, can I enumerate the list of available Java packages? How?

孤街醉人 提交于 2019-12-07 14:47:53

问题


I tried doing this:

var r = {};
for (p in Packages) {
  if (Packages.hasOwnProperty(p)) {
    r[p] = p.toString();
  }
}

...But I got an error:

TypeError: Invalid iterator value ... at line xx

The line indicated in the error message is the one with

for (p in Packages) { ... } 

Also,

var r = {
  "Packages" : typeof Packages,
  "Packages.length" : typeof Packages.length,
  "Packages.java" : typeof Packages.java,
  "Packages.surelyThisIsNotDefined" : typeof Packages.surelyThisIsNotDefined
};

results in:

{
  "Packages": "object",
  "Packages.length": "object",
  "Packages.java": "object",
  "Packages.surelyThisIsNotDefined": "object"
}

...which is no help at all.


回答1:


Not sure about the format of output you want, the following code return a list of Java Packages Loaded by the ClassLoader at runtime:

packs = java.lang.Package.getPackages()
for (p in packs) 
print(packs[p]);


来源:https://stackoverflow.com/questions/15777059/in-rhino-can-i-enumerate-the-list-of-available-java-packages-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!