Groovy スクリプト createWidget.groovy があります。
import com.example.widget
Widget w = new Widget()
このスクリプトは、次のように実行するとうまく動作します。
$ groovy -cp /path/to/widget.jar createWidget.groovy
しかし、スクリプト内にクラスパスをハードコーディングして、ユーザーがその場所を知る必要がないようにしたかったので、createWidget.groovy を次のように変更しました (これは、groovy でクラスパスを変更する方法の 1 つです)。
this.getClass().classLoader.rootLoader.addURL(new File("/path/to/widget.jar").toURL())
import com.example.widget
Widget w = new Widget()
しかし、これは常にインポート時の実行時エラーで失敗します: unable to resolve class com.example.widget
.
これは正統ではないように見えますが、インポートの前に rootLoader をいじることはできないと思いますか、それとも何か他のものですか?