我可以编译一个名称与类不同的 java 文件吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1841847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I compile a java file with a different name than the class?
提问by Hulk
Is there any way to compile a java program without having the java file name with its base class name.
有没有办法编译java程序而没有java文件名及其基类名。
If so, please explain..
如果是这样,请解释..
采纳答案by Steven Schlansker
Your Java file name should always reflect the public class defined within that file. Otherwise, you will get a compiler error. For example, test.java:
您的 Java 文件名应始终反映该文件中定义的公共类。否则,您将收到编译器错误。例如,test.java:
public class Foo {}
Trying to compile this gives:
试图编译这个给出:
[steven@scstop:~]% javac test.java
test.java:1: class Foo is public, should be declared in a file named Foo.java
public class Foo {
^
1 error
So you must have your filename match your public class name, which seems to render your question moot. Either that or I don't understand what you're asking... spending some time explaining what you are actually trying to achieve would go a long way towards asking a more effective question :)
所以你必须让你的文件名与你的公共类名相匹配,这似乎使你的问题没有实际意义。要么是那样,要么我不明白你在问什么......花一些时间解释你真正想要实现的目标会大大有助于提出一个更有效的问题:)
回答by Kalpak
I guess what he means is the .java file is named differently than the actual class defined inside it.
我猜他的意思是 .java 文件的名称与其中定义的实际类不同。
I guess this is not possible.
我想这是不可能的。
回答by Joel
No, the public class name must match the file name. Inner, non public, class names may differ.
不,公共类名必须与文件名匹配。内部的、非公共的、类名可能不同。
回答by Bill K
You must have a public class with the same name as the file name. This is a Very Good Thing. You CAN have secondary classes inside the same file as long as they are not public. They can still be "default" though, so they can still be used by other classes in the same package.
您必须有一个与文件名同名的公共类。这是一件非常好的事情。你可以在同一个文件中有二级类,只要它们不是公共的。但是它们仍然可以是“默认的”,因此它们仍然可以被同一包中的其他类使用。
This should not be done for the most part. Java's naming patterns regarding classes and packages are one of the bigger advantages it has--makes a programmers life easier at no cost.
大多数情况下不应该这样做。Java 关于类和包的命名模式是它拥有的更大优势之一——让程序员的生活更轻松,而且不花钱。
回答by Chad Okere
No. You could write a shell script to rename the .java file before compiling it, but javac requires that filenames = class names.
不可以。您可以在编译之前编写一个 shell 脚本来重命名 .java 文件,但 javac 要求文件名 = 类名。
(Also in windows, it's case insensitive, so ASDF.java can compile Asdf.class)
(同样在windows中,不区分大小写,所以ASDF.java可以编译Asdf.class)
回答by Peter Lawrey
You can use the Java Compile API and compile any java source you wish, the source need not come from a file or could come from a file with an unrelated name. It depends on how obtuse you want to develop your program. ;)
您可以使用 Java Compile API 并编译您希望的任何 Java 源代码,源代码不必来自文件或可以来自具有无关名称的文件。这取决于你想开发你的程序有多迟钝。;)
回答by ved
yes, you can choose any name for the file (.java). there is no matter to what are the name of classes in that file means that class names may be totaly different from the file name. you should compile the program with file name and you should run the program with the class name in which the main method exist. main methods may be multiple in different classes so you should run it with the class name in which the main method you want to run......
是的,您可以为文件选择任何名称 (.java)。无论该文件中的类名是什么,都意味着类名可能与文件名完全不同。您应该使用文件名编译程序,并使用 main 方法所在的类名运行程序。main 方法在不同的类中可能有多个,所以你应该使用你想要运行的 main 方法的类名来运行它......
回答by ved
To answer the question take a look at this example:
Create a file Sample.java
要回答这个问题,请看这个例子:
创建一个文件 Sample.java
class A
{
public static void main(String args[])
{
String str[] = {""};
System.out.println("hi");
B.main(str);
}
}
class B
{
public static void main(String args[])
{
System.out.println("hello");
}
}
now you compile it as javac Sample.java
and run as java A
then output will be
现在您将其编译为javac Sample.java
并运行,java A
然后输出将是
hi
hello
or you run as java B
then output will be hello
或者你运行,java B
然后输出将是hello
Notice that none of the classes are marked public
therefore giving them default
access. Files without any public classes have no file naming restrictions.
请注意,没有任何类被标记,public
因此允许它们default
访问。没有任何公共类的文件没有文件命名限制。
回答by user442317
we can save the file tootle different name of class name because in java we compile the program but we run the method. we have to compile our program with file name and run our class name
我们可以用不同的类名保存文件,因为在java中我们编译程序但我们运行方法。我们必须用文件名编译我们的程序并运行我们的类名
回答by Nithish Reddy J
Yes,it is possible to compile a java source file with different file name but you need to make sure none of the classes defined inside are public...when you compile the source file the corresponding .class files for the classes inside the source file are created.
是的,可以用不同的文件名编译一个 java 源文件,但你需要确保里面定义的类都不是公共的......当你编译源文件时,源文件中类的相应 .class 文件被创建。