简体   繁体   English

无法在Nixos上运行Groovy脚本

[英]Cannot run groovy script on nixos

I'm trying to run this groovy script from nixos 我正在尝试从nixos运行此groovy脚本

#!/usr/bin/env groovy

@Grapes(
    @Grab(group='net.java.dev.jna', module='jna-platform', version='4.5.0')
)

import com.sun.jna.platform.unix.X11

def display = X11.INSTANCE.XOpenDisplay(null)
if(display == null) {
    throw new IllegalStateException('Can\'t open default display')
}
def root = X11.INSTANCE.XRootWindow(display, X11.INSTANCE.XDefaultScreen(display))
if(root == null) {
    throw new IllegalStateException('Can\'t find root window')
}


if(display != null) {
    X11.INSTANCE.XCloseDisplay(display)
}

Which results in the following exception 导致以下异常

Caught: java.lang.UnsatisfiedLinkError: Unable to load library 'X11': Native library (linux-x86-64/libX11.so) not found in resource path ([file:/etc/user/john/.groovy/grapes/net.java.dev.jna/jna-platform/jars/jna-platform-4.5.0.jar, file:/etc/user/john/.groovy/grapes/net.java.dev.jna/jna/jars/jna-4.5.0.jar]) java.lang.UnsatisfiedLinkError: Unable to load library 'X11': Native library (linux-x86-64/libX11.so) not found in resource path ([file:/etc/user/john/.groovy/grapes/net.java.dev.jna/jna-platform/jars/jna-platform-4.5.0.jar, file:/etc/user/john/.groovy/grapes/net.java.dev.jna/jna/jars/jna-4.5.0.jar])
        at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:303)
        at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:427)
        at com.sun.jna.Library$Handler.<init>(Library.java:179)
        at com.sun.jna.Native.loadLibrary(Native.java:569)
        at com.sun.jna.Native.loadLibrary(Native.java:544)
        at com.sun.jna.platform.unix.X11.<clinit>(X11.java:417)
        at helloX11.run(helloX11:10)

How can I setup the jvm on nixos to point to libX11.so correctly? 如何在nixos上设置jvm以正确指向libX11.so?

This also happens with a JVM that is supposed to be configured with X11. 对于应该使用X11配置的JVM,也会发生这种情况。 It seems that the native libraries are not propagated via CLASSPATH . 似乎本机库不是通过CLASSPATH传播的。 This should probably be fixed in NixPkgs. 这可能应该在NixPkgs中修复。 In the meanwhile, you can use the following to make a native package available using nix-shell . 同时,您可以使用以下命令使用nix-shell使本机软件包可用。

nix-shell -E 'with import <nixpkgs> { }; runCommand "dummy" { buildInputs = [ groovy ]; shellHook = "export CLASSPATH=${xlibs.libX11.out}/lib"; } ""' --run ./x11script.groovy

( nix-shell -E uses attributes from a dummy derivation as produced here by the invocation of runCommand ) nix-shell -E使用来自虚拟派生的属性,如通过调用runCommand产生的runCommand

If it's an option for you, you can put the dummy derivation in a file called deps.nix and change the hashbang and first lines of your script. 如果您愿意,可以将虚拟派生文件放在名为deps.nix的文件中,并更改脚本的hashbang和第一行。

with import <nixpkgs> { };
runCommand "dummy" {
  buildInputs = [ groovy ];
  shellHook = "export CLASSPATH=${xlibs.libX11.out}/lib";
} ""

Top of your script: 脚本顶部:

#!/usr/bin/env nix-shell
/*
#!nix-shell -i groovy
#!nix-shell deps.nix
*/

Now you can call the script without invoking nix-shell manually on the command line. 现在,您可以调用脚本,而无需在命令行上手动调用nix-shell

It seems to me that a better solution may be possible using NixPkgs' setup hooks mechanism, but sadly that's not something I can help you with now. 在我看来,使用NixPkgs的设置挂钩机制可能会找到更好的解决方案,但可惜现在我无法为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM