3

我正在寻找一种从预先写入的内存位置读取数据的方法。有没有办法在 Java 中,在 Windows 上运行的计算机上做到这一点?

4

1 回答 1

3

Java provides an "abstraction" of a computer, hence "Java virtual machine" (JVM). This JVM is (theoretically) the same regardless of the underlying hardware or operating system. So (AFAIK) no direct access to low-level memory is possible from program space.

My suggestion would be to write something in a lower-level language (C for instance makes mem access fairly easy), then use the Java Native Interface (JNI) to call it and get the data into your Java program. Google on "Java Native Interface" and/or "Calling C From Java" for tutorial on how to. Obviously this native component would be compiled differently for each platform you want to run it on, providing a separate executable binary "native" file. ANSI C used to be a good standard to code to, since compilers complying with this standard are available on all(??) platforms, if you want to go multi-platform.

Using a JNI call will break the "compile once, run anywhere"-feature of your Java, as it depends on a component that still needs to be recompiled for each platform you want to run it on. And, of course, in practice the native code will probably also differ from platform to platform, as memory organisation might differ between platforms.

于 2013-04-05T08:33:43.800 回答