问题
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