VMSystem在Java类库中是什么?

huangapple go评论75阅读模式
英文:

What is VMSystem in the Java class library?

问题

以下是翻译好的部分:

在我探索编程语言/编译器的过程中,我认为尝试编写Java类库的简化版本可能会很有趣。

我决定首先尝试编写自己的println()方法。不,不是一个简单调用println的方法。

我的代码到目前为止:

package maintest;

import java.io.PrintStream;
import gnu.classpath.SystemProperties;
import gnu.classpath.VMStackWalker;

public class JohnSystem {
	
    public static final PrintStream out = VMSystem.makeStandardOutputStream();
}

所以基本上,我写了一个“新”的System类。关于OpenJDK系统类,我注意到一个有趣的事情。Java.lang.System内的public static final变量'out'并不是通过通常使用new关键字实例化对象的方式来实例化的。我尝试模仿它的方式,并相应地编写了上面的代码。

我认为VMSystem是类库中的另一个类,它有一个静态方法makeStandardOutputStream(),该方法返回一个PrintStream对象。

这本来没什么问题,但是编译器(或者可能是Eclipse?)不识别VMSystem,当我尝试导入我在OpenJDK中找到的包时,它们也没有被识别。

我所指的代码位于:http://developer.classpath.org/doc/java/lang/System-source.html

所以,基本上我正在努力找出如何能够使用VMSystem方法。也许需要下载某个包吗?但这似乎不太合理。因为System.out.println()是可用的,所以这些包肯定已经存在了,不是吗?还是可能是权限/安全问题?

谢谢!

英文:

In my journey to understand programming languages/compilers, I thought it might be cool to try to write small versions of the Java class library.

I decided to start with writing my own println() method. And no, not a method that simply calls println.

My Code so far:

package maintest;

import java.io.PrintStream;
import gnu.classpath.SystemProperties;
import gnu.classpath.VMStackWalker;

public class JohnSystem {
 
    public static final PrintStream out = VMSystem.makeStandardOutputStream();

}

So basically, I wrote a "new" System class. Now something funny I noticed about the OpenJDK system class. The public static final variable 'out' contained within Java.lang.System doesn't get instantiated in the way objects normally do with the new keyword. I tried to mimic the way it was done and wrote the code above accordingly.

I figured VMSystem was another class in the class library which had a static method makeStandardOutputStream() which returned a PrintStream object.

That would be fine except the compiler (or maybe eclipse?) doesn't recognize VMSystem and when I tried to import the packages I found at the OpenJDK, they are also not recognized.

The code I'm referring to is at: http://developer.classpath.org/doc/java/lang/System-source.html

So, basically I'm trying to find out how to be able to use the VMSystem methods. Is there maybe a package I need to download? That doesn't really make sense though. Since System.out.println() works, then the packages must already be there no? Or is it perhaps a permissions/security issue?

Thanks!

答案1

得分: 2

VMSystem类是(旧版、几乎废弃的)GNU ClassPath Java SE类库实现中的一个内部类。

它不是Java API的一部分,在OpenJDK中也没有相当于它的东西;即你寻找的方法可能不存在。

另一方面……如果你想了解OpenJDK如何处理JVM引导(例如设置inouterr),最好的方法是下载实际的OpenJDK源代码树,并进行深入研究。

例如,在Java 11中,inouterr的初始化发生在System的一个名为initPhase1()的私有方法中。当然,你无法调用它(因为它是private),但你可以查看它的功能,并在自己的实验中复制它。但你会看到,这些变量的实际赋值是在本地代码中完成的。

最后:

在我探索编程语言/编译器的过程中,我觉得尝试编写Java类库的小版本可能会很酷。

我认为你最好只是阅读OpenJDK源代码。

英文:

The VMSystem class is an internal class in the (old, virtually defunct) GNU ClassPath implementation of the Java SE class libraries.

It is not part of the Java APIs, and there is no equivalent to it in OpenJDK; i.e. the methods you are looking for may not exist.

On the other hand ... if you want to understand how OpenJDK handles JVM bootstrapping (setting up in, out and err for example), your best bet is to download an actual OpenJDK source tree, and deep-dive it.

In Java 11 for example, the initialization of in, out and err happens in a private method of System called initPhase1(). Naturally, you can't call it (because it is private), but you can certainly look to see what it does and duplicate that in your own experiment. But you will see that the actual assignment of the those variables happens in native code.

Finally:

> In my journey to understand programming languages/compilers, I thought it might be cool to try to write small versions of the Java class library.

I think you would be better of just reading the OpenJDK source code.

huangapple
  • 本文由 发表于 2020年10月18日 11:19:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64409390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定