Does the class className need to be encoded in className.java?

I use maven to build the project and compile the crash because I put class Test2 in Test.java .

but is it because of maven or simply because Java itself does not support this?

By the way, how can I open a maven project using eclipse?

+4
source share
5 answers

A public class called ClassName should (in most cases) be defined in a file called ClassName.java .

Edit

Although this is not a requirement of the Java language, most (if not all) Java implementations, the aforementioned association between the class name and the file name must be executed, or a compiler error will occur.

(For more information, see Jon Skeet's answer .)

The reason for this can be found by reading Section 7.2: Packages Java Language Specification, Third Edition .

This section describes how the directory structure can be mapped to the fully qualified class name in Java, which leads to the .class file that contains the bytecode for the class being in a path that looks like the path to the fully qualified name class.

The original answer, which incorrectly indicated that the naming scheme is a requirement, has been edited.

+6
source

The Java language specification itself does not actually require this, but it explicitly allows a file system-based implementation and does most.

From section 7.6 :

When packages are stored in a system file (Β§7.2.1), the host system may choose to enforce a constraint, which is a compile-time error, if the type is not found in the file under the name consisting of the type name plus extension (for example, .java or .jav ) if one of the following conditions is true:

  • The type is referred to by code in other compilation units of the package in which the type is declared.
  • the type is declared open (and therefore potentially available from code to other packages).

This limitation implies that there must be at most one such type for a compilation unit. This restriction simplifies the compiler for programming in Java language or for implementing the Java Virtual Machine to search for the name of a class inside a package; for example, the source code for the public type wet.sprocket.Toad can be found in the Toad.java file in the wet / asterisk directory and the corresponding object code will be found in the Toad.class file in the same directory.

When packets are stored in a database (Β§7.2.2), the host system should not impose such restrictions. In practice, many programmers choose each class or interface in their own compilation unit, regardless of whether it is public or referenced in other compilation units.

For practical purposes, I consider it reasonable to assume that this is necessary.

+6
source

Java requires your public class to be inside the file with the same name.

For eclipse and maven use the sonatype m2 plugin. Inside your maven project you can enter

 mvn eclipse:eclipse 

and maven will create a .project and .classpath file for you. This eclipse files should work with the project.

You must define the eclipse M2_REPO classpath variable in the path of your local maven repository.

With the m2 sonotype, you can do maven things from within an eclipse: add dependencies, run maven targets, ...

+2
source

I use maven to build the project, and failed to compile because I put the Test2 class in Test.java,

The source files must be specified after the public class that they contain, adding the suffix .java . In your case, the source file for the open Test2 class should be Test2.java 1 .

Is it because of maven or simply because Java itself does not support it?

The Java compiler, javac , complains, not Maven (Maven does not change the behavior of the compiler).

how can i open maven project using eclipse?

Use the Maven Eclipse Plugin (i.e. the plugin for Maven) and just run mvn eclipse:eclipse in your project and then Import ... as Existing projects into the workspace in Eclipse. You will need to set the classpath variable M2_REPO, indicating the use of the local repository. See Usage for more details.

Or (and that is exclusive or) install m2eclipse (that is, a plug-in for Eclipse that expands so that it can understand Maven projects and interact with it bi-directionally) and Import ... your project as Existing Maven projects .. p>

If EXCLUSIVE is not clear enough, it means: use one or the other, but not both at the same time.

1 As John Skeet mentioned, JLS allows this limitation for file implementations. This does not apply when using a database to store Java constructs (for example, Visual Age for Java). But in your case it is.

0
source

The connection between the file name and the class name is as follows:

  • If you have a public class in the file, the file name must match the class name
  • you can have as many non-public classes as you want in a file with different names
0
source

Source: https://habr.com/ru/post/1309909/


All Articles