简体   繁体   English

主方法的奇怪行为

[英]Weird behavior with main method

I have a class like - 我有一个类 -

public class Test{
    public static void main(String[] args){
        for(String s: args){
            System.out.println(s);
        }
    }
}

When I ran this program like > java Test * it print the names of all files in that folder. 当我像> java Test *一样运行这个程序时,它会打印该文件夹中所有文件的名称。
I don't know whether JVM or windows passes this array instead of ' * ', I would like to know whether this program behaves same on the other platforms(other than windows) and why it behaves in this way? 我不知道JVM或Windows是否通过这个数组而不是'*',我想知道这个程序在其他平台上是否表现相同(除了windows)以及它为什么会以这种方式运行? is there any purpose or reason for this? 这有什么目的或原因吗?

NOTE : 注意
It doesn't break any of my code but I'm just curious. 它没有破坏我的任何代码,但我只是好奇。

I originally thought that this was just the command line shell performing filename expansion. 我原本以为这只是执行文件名扩展的命令行shell。 That's what I'd expect under a Unix-like system, but not Windows... but the same occurs for me under Windows, too. 这就是我在类Unix系统中所期望的,但不是Windows ......但在Windows下也是如此。

It doesn't occur when I write the equivalent C# code... so I suspect it's something in the Java executable launcher doing it. 当我编写等效的C#代码时,它不会发生......所以我怀疑它是Java可执行启动程序中的一部分。 (Note that javaw.exe behaves the same way as java.exe.) (请注意,javaw.exe的行为与java.exe相同。)

It certainly came as a surprise to me, and I didn't think older versions of Java did this on Windows, although this mailing list post from December 2000 suggests I'm wrong. 这当然让我感到意外,我不认为 Java的旧版本在Windows上做到这一点,尽管从2000年12月开始的这个邮件列表表明我错了。 (I'm using an OpenJDK 1.7 JRE.) (我使用的是OpenJDK 1.7 JRE。)

I can't find any description of this general case in the Java Windows documentation - it mentions expansion of classpath entries, but not general arguments. 我在Java Windows文档中找不到这种一般情况的任何描述 - 它提到了类路径条目的扩展,但没有提到一般参数。

Not until recently (i guess in Java 6) a new feature was added to Java, where '*' is translated to all files in the current directory. 直到最近(我猜在Java 6中),Java中添加了一个新功能,其中'*'被转换为当前目录中的所有文件。 This was mainly to avoid need for passing long class paths and giving a platform independent way to pass all jar files in a folder to a classpath. 这主要是为了避免需要传递长类路径并提供一种独立于平台的方式将文件夹中的所有jar文件传递给类路径。

java -cp "lib/*" <mainClass>

lib/* would be tranlated to list of file separated by the classpath separator for that platform (ie : for unix like and ; for windows) passed to -cp property. lib / *将被转换为由该平台的类路径分隔符分隔的文件列表(即:对于unix like和; for windows)传递给-cp属性。

I think this might be the reason for the behavior that you see. 我认为这可能是你看到的行为的原因。 I assumed that it was only for specifying classpaths but now I think I was wrong then. 我认为它只是用于指定类路径,但现在我认为我错了。

I think command line, first interpret the special operators before passing to java file. 我想命令行,首先在传递给java文件之前解释特殊操作符。

eg 例如

java Test * > test.txt

java Test Test.????
java Test ^

I think its not specific to java. 我认为它不是特定于java。

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

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