【问题标题】:Run JTape Library on Java (java library path)在 Java 上运行 JTape 库(java 库路径)
【发布时间】:2014-05-09 11:47:55
【问题描述】:

我正在尝试使用JTape Library 从 DDS4 磁带读取一些数据。 我想用 eclipse 在 Linux 12.04 LTS 下运行我的代码

问题是我不能让 eclipse 以任何方式引用 TapeLinux.c 库。

问题:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no TapeLinux in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at BasicTapeDevice.<clinit>(BasicTapeDevice.java:169)
at TestEOD.main(TestEOD.java:12)

这是我的课程:

/* TestEOD.java */

import java.io.*;

public class TestEOD {
public static void main(String[] args) throws IOException {
   /* if (args.length != 1) {
        System.err.println("Usage: java TestEOD <path to device>");
        System.exit(1);
    }*/

    BasicTapeDevice d = new BasicTapeDevice("/dev/nst0");

    System.out.print("Rewinding...");
    System.out.flush();
    d.rewind();
    System.out.println("done!");

    System.out.print("Spacing to end of data...");
    System.out.flush();
    d.spaceEOD();
    System.out.println("done!");
}
}


/* BasicTapeDevice.java */

import java.io.*;
public class BasicTapeDevice {
private FileDescriptor fd;
private InputStream in;
private OutputStream out;
private boolean eof;
private boolean eom;
private boolean ignoreEOM;

public BasicTapeDevice(String pathName) throws IOException {
    fd = new FileDescriptor();
    tapeOpen(pathName);
    in = new TapeInputStream();
    out = new TapeOutputStream();
    eof = false;
    eom = false;
    ignoreEOM = false;
}
public synchronized void close() throws IOException {
    if (fd != null) {
        try {
            if (fd.valid()) {
                tapeClose();
            }
        } finally {
            fd = null;
        }
    }
}
public InputStream getInputStream() throws IOException {
    ensureOpen();
    return in;
}
public OutputStream getOutputStream() throws IOException {
    ensureOpen();
    return out;
}
public int getBlockSize() throws IOException {
    ensureOpen();
    return tapeGetBlockSize();
}
public void setBlockSize(int bs) throws IOException {
    ensureOpen();
    tapeSetBlockSize(bs);
}
public void rewind() throws IOException {
    ensureOpen();
    tapeRewind();
}
public void spaceEOD() throws IOException {
    ensureOpen();
    tapeSpaceEOD();
}
public void clearEOF() throws IOException {
    ensureOpen();
    if (eof) {
        eof = false;
        /* assume that the file mark has already been skipped */
    } else { 
        throw new IOException("not at end of file");
    }
}
public void clearEOM() throws IOException {
    ensureOpen();

    if (eom) {
        ignoreEOM = true;
    } else {
        throw new IOException("not at logical end of media");
    }
}
class TapeInputStream extends InputStream {
    private byte[] temp = new byte[1];
    public int read() throws IOException {
        int n = read(temp, 0, 1);
        if (n <= 0) {
            return -1;
        }
        return temp[0] & 0xff;
    }
    public int read(byte[] b, int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        }
        if (off < 0 || len < 0 || off+len > b.length) {
            throw new IndexOutOfBoundsException();
        }
        if (len == 0) {
            return 0;
        }
        if (eof) {
            return -1;
        }
        ensureOpen();
        int n = tapeRead(b, off, len);
        if (n <= 0) {
            return -1;
        }
        return n;
    }
    public long skip(long numbytes) throws IOException {
        return 0;
    }
    public void close() throws IOException {
        BasicTapeDevice.this.close();
    }
}
class TapeOutputStream extends OutputStream {
    private byte[] temp = new byte[1];

    public void write(int b) throws IOException {
        temp[0] = (byte) b;
        write(temp, 0, 1);
    }
    public void write(byte[] b) throws IOException {
        write(b, 0, b.length);
    }
    public void write(byte[] b, int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();

        }
        if (off < 0 || len < 0 || off+len > b.length) {
            throw new IndexOutOfBoundsException();
        }
        if (eom && !ignoreEOM) {
            throw new LogicalEOMException("logical end-of-media");
        }
        int n = tapeWrite(b, off, len);
        while (n < len) {
            n += tapeWrite(b, off + n, len - n);
        }
    }
    public void close() throws IOException {
        BasicTapeDevice.this.close();
    }
}
protected void finalize() {
    try {
        close();
    } catch (IOException ex) {
    }
}
private void ensureOpen() throws IOException {
    if (fd == null || !fd.valid()) {
        throw new IOException("tape device is not open");
    }
}
private static native void initFields();
private native void tapeOpen(String pathName) throws IOException;
private native void tapeClose() throws IOException;
private native int tapeRead(byte[] b, int off, int len) throws IOException;
private native int tapeWrite(byte[] b, int off, int len) throws IOException;
private native int tapeGetBlockSize() throws IOException;
private native void tapeSetBlockSize(int bs) throws IOException;
private native void tapeRewind() throws IOException;
private native void tapeSpaceEOD() throws IOException;

/* load the JNI library specific for this platform */
static {
    StringBuffer buf = new StringBuffer("Tape");
    String osName = System.getProperty("os.name");
    if (osName.equals("Windows NT") || osName.equals("Windows 2000")) {
        buf.append("WinNT");
    } else {
        buf.append(osName);
    }
    System.loadLibrary(buf.toString());
    initFields();
}
}

我已经尝试过什么

我环顾四周,我需要包含包含文件 TapeLinux.c 的文件夹的路径

我已经尝试了所有these answers 并没有任何改变

你能帮我弄清楚在这种情况下如何使用 JNI 以及我应该配置什么来运行我的代码

谢谢

【问题讨论】:

    标签: java c dll java-native-interface


    【解决方案1】:

    由于您使用的是 Linux,消息 ...main java.lang.UnsatisfiedLinkError: no TapeLinux in java... 可能意味着名为 libTapeLinux.so 的库找不到。

    您似乎正在通过尝试定位 TapeLinux.c 来寻找解决方案,并且您应该搜索 libTapeLinux.so 并且一旦找到它,请确保 libTapeLinux.so 在加载路径上。

    【讨论】:

    • 非常感谢,我已经解决了这个问题,你提到的问题是我遇到的问题之一。另一个问题是生成文件中的错误。无论如何,谢谢你的帮助我接受你的回答,因为至少你试图帮助我:)
    • 很高兴听到您找到了解决方案 - 顺便说一句,是否正在制作低俗小说第 2 部分?多么棒的电影。
    猜你喜欢
    • 2018-11-07
    • 2021-07-08
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多