更新时间:2022-10-22 17:05:07
h2> java< class-name>
命令语法
首先,您需要了解正确的方法使用 java
(或 javaw
)命令启动程序。
正常的语法 1 是这样的:
java [< option> ...]< class-name> [< argument> ...]
其中< option>
是一个命令行选项(以 - 字符开头),< class-name>
是一个完全限定的Java类名, < argument>
是传递给应用程序的任意命令行参数。
1 - executableJAR文件还有第二种语法
完全限定的类名通常写成你在Java源代码中的样子;例如
packagename.packagename2.packagename3.ClassName
但是某些版本的 java
命令允许使用斜杠而不是句点;例如
packagename / packagename2 / packagename3 / ClassName
(令人困惑的)看起来像一个文件路径名,但不是一个。请注意,术语完全限定类名是标准的Java术语...不是我刚刚弥补的: - )
java
命令应该类似的示例:
Xmx100m com.acme.example.ListUsers fred joe bert
上述将导致 java
命令执行以下操作:
com.acme.example.ListUsers
class。主要
带签名+修饰符的方法 public static void main(String [])
。 String []
传递。
当您收到消息找不到或加载主类...,这意味着第一步失败。 java
命令无法找到该类。事实上,消息中的...将是 java
正在查找的完全限定类名。
那么为什么可能找不到类呢?基本上,有两个主要原因:
第一个可能的原因是你可能提供了错误的类名。考虑到上面的例子,这里有各种错误的方法来指定类名:
示例#1 - 一个简单的类名:
java ListUser
当在类如 com.acme中声明类时。示例
,则必须在 java
命令中使用完整的类名,包括例如
java com.acme.example.ListUser
示例#2 - 文件名或路径名,而不是类名称:
java ListUser.class
java com / acme / example / ListUser.class
示例#3 - 包含不正确的类名:
java com.acme.example.listuser
示例#4 - 错字
java com.acme.example.mistuser
第二个可能的原因是类名是正确的,但是 java
命令找不到类。要理解这一点,你需要理解classpath的概念。这在Oracle文档中有详细解释:
所以...如果您正确指定了类名,接下来要检查的是您正确指定了classpath:
java
命令时生效的CLASSPATH环境变量。 java
命令时,它是有效的。
当你在classpath上放置一个目录时,它就是符合命名空间的根。类位于根目录下的目录结构中,通过将完全限定名映射到路径名。因此,例如,如果/ usr / local / acme / classes在类路径上,那么当JVM查找 com.acme.example.Foon
,它将查找具有此路径名的.class文件:
/ usr / local / acme / classes / acme / example / Foon.class
如果你输入/ usr / local / acme / classes /
类路径需要包括所有的
em>其他(非系统)类。 (系统类自动定位,您很少需要关心这一点。)
java -jar< jar file>
语法用于可执行JAR文件的替代语法如下: p>
java [< option> ...] -jar< jar-file-name> [< argument> ...]
例如
java -Xmx100m -jar /usr/local/acme-example/listuser.jar fred
在这种情况下,入口点类的名称(即 com.acme.example.ListUser
)和类路径在JAR的MANIFEST中指定文件
典型的Java IDE支持在IDE JVM中运行Java应用程序,一个子JVM。因为IDE使用自己的机制来构造运行时类路径,识别主类并创建 java
,所以这些通常免于此特定异常,命令行。
但是,如果你在IDE后面做事情打破事情,仍然可能发生此异常。例如,如果您以前在Eclipse中为Java应用程序设置了一个应用程序启动器,然后将包含main类的JAR文件移动到文件系统中的不同位置,而不告诉Eclipse ,Eclipse会不经意地启动带有不正确类路径的JVM。
简而言之,如果在IDE中遇到这个问题,请检查类似陈旧的IDE状态,或破坏的启动程序配置。
A common problem that new Java developers experience is that their programs fail to run with the error message: Could not find or load main class ...
What does this mean, what causes it, and how should you fix it?
java <class-name>
command syntaxFirst of all, you need to understand the correct way to launch a program using the java
(or javaw
) command.
The normal syntax1 is this:
java [ <option> ... ] <class-name> [<argument> ...]
where <option>
is a command line option (starting with a "-" character), <class-name>
is a fully qualified Java class name, and <argument>
is an arbitrary command line argument that gets passed to your application.
1 - There is a second syntax for "executable" JAR files which I will describe at the bottom.
The fully qualified classname is conventionally written as you would in Java source code; e.g.
packagename.packagename2.packagename3.ClassName
However some versions of the java
command allow you to use slashes instead of periods; e.g.
packagename/packagename2/packagename3/ClassName
which (confusingly) looks like a file pathname, but isn't one. Note that the term fully qualified classname is standard Java terminology ... not something I just made up to confuse you :-)
Here is an example of what a java
command should look like:
java -Xmx100m com.acme.example.ListUsers fred joe bert
The above is going to cause the java
command to do the following:
com.acme.example.ListUsers
class.main
method with signature + modifiers public static void main(String[])
.String[]
.
When you get the message "Could not find or load main class ...", that means that the first step has failed. The java
command was not able to find the class. And indeed, the "..." in the message will be the fully qualified class name that java
is looking for.
So why might it be unable to find the class? Basically, there are two main causes:
The first likely cause is that you may have provided the wrong class name. (Or ... the right class name, but in the wrong form.) Considering the example above, here a variety of wrong ways to specify the class name:
Example #1 - a simple class name:
java ListUser
When the class is declared in a package such as com.acme.example
, then you must use the full classname including the package name in the java
command; e.g.
java com.acme.example.ListUser
Example #2 - a filename or pathname rather than a class name:
java ListUser.class
java com/acme/example/ListUser.class
Example #3 - a class name with the casing incorrect:
java com.acme.example.listuser
Example #4 - a typo
java com.acme.example.mistuser
The second likely cause is that the class name is correct, but that the java
command cannot find the class. To understand this, you need to understand the concept of the "classpath". This is explained well by the Oracle documentation:
java
command documentationSo ... if you have specified the class name correctly, the next thing to check is that you have specified the classpath correctly:
java
command. Check that the directory names and JAR file names are correct.java
command.Additional Notes:
When you put a directory on the classpath, it notionally corresponds to the root of the qualified name space. Classes are located in the directory structure beneath that root, by mapping the fully qualified name to a pathname. So for example, if "/usr/local/acme/classes" is on the class path, then when the JVM looks for a class called com.acme.example.Foon
, it will look for a ".class" file with this pathname:
/usr/local/acme/classes/com/acme/example/Foon.class
If you had put "/usr/local/acme/classes/com/acme/example" on the classpath, then the JVM wouldn't be able to find the class.
The classpath needs to include all of the other (non-system) classes that your application depends on. (The system classes are located automatically, and you rarely need to concern yourself with this.)
java -jar <jar file>
syntaxThe alternative syntax used for "executable" JAR files is as follows:
java [ <option> ... ] -jar <jar-file-name> [<argument> ...]
e.g.
java -Xmx100m -jar /usr/local/acme-example/listuser.jar fred
In this case the name of the entry-point class (i.e. com.acme.example.ListUser
) and the classpath are specified in the MANIFEST of the JAR file.
A typical Java IDE has support for running Java applications in the IDE JVM itself or in a child JVM. These are generally immune from this particular exception, because the IDE uses its own mechanisms to construct the runtime classpath, identify the main class and create the java
command line.
However it is still possible for this exception to occur, if you do things behind the back of the IDE to break things. For example, if you have previously set up an Application Launcher for your Java app in Eclipse, and you then moved the JAR file containing the "main" class to a different place in the file system without telling Eclipse, Eclipse would unwittingly launch the JVM with an incorrect classpath.
In short, if you get this problem in an IDE, check for things like stale IDE state, broken project references or broken launcher configurations.