Running Java Program From Command Line(从命令行运行 Java 程序)
问题描述
所以我在这里有一个菜鸟时刻,我以前从未使用过命令行来运行 java 程序,但我现在需要.我遇到的问题是,当我尝试运行程序时,我得到了 ClassNotFoundException.我的课程叫做 OmadUpdate.我已经使用 javac 命令将 OmadUpdate.java 文件编译到 OmadUpdate.class 中.我检查了目录,它们肯定都在那里,但是当我运行 java OmadUpdate 命令时,它给了我一条错误消息,说
so I am having a noob moment here, I haven't ever used the command line to run a java program before but I need to right now. The problem I am having is that when I try to run the program I get a ClassNotFoundException. My class is called OmadUpdate. I already have compiled the OmadUpdate.java file into OmadUpdate.class using the javac command. I have checked the directory and they are both definitely there, however when I run the java OmadUpdate command, it gives me an error message saying
Exception in thread "main" java.lang.NoClassDefFoundError: OmadUpdate (wrong name: org/openmetadata/main/OmadUpdate)
......
......
Could not find the main class: OmadUpdate. Program will exit
但它就在目录中.当我键入 dir 时,我同时拥有 OmadUpdate.class 和 OmadUpdate.java.我什至尝试过使用java org.openmetadata.main.OmadUpdate",因为这是它所在的包名.我难住了.感谢您的帮助.
But its right there in the directory. When I type dir I have both OmadUpdate.class and OmadUpdate.java. I have even tried using "java org.openmetadata.main.OmadUpdate" because that is the package name that it is under. I am stumped. Thanks for the assistance.
推荐答案
您的类似乎已在 org.openmetadata.main
包中声明.
Your class appears to have been declared in the org.openmetadata.main
package.
要让java正确加载类,它需要位于与包结构匹配的正确目录结构中.
To get java to load the class correctly, it needs to be in the correct directory structure that matches the package structure.
所以 org.openmetadata.main.OmadUpdate
的类文件应该在目录 orgopenmetadatamain
中.
So the classfiles for org.openmetadata.main.OmadUpdate
should be in the directory orgopenmetadatamain
.
然后,当你运行 java
命令时,这个目录结构的根目录应该在类路径上 - 举个简单的例子,这只是意味着你的当前目录应该是 orgopenmetadatamain
.
Then, when you run the java
command, the root of this directory structure should be on the classpath - for a simple example this just means that your current directory should be the parent directory of orgopenmetadatamain
.
运行 java
时,您需要使用句点而不是斜杠指定完整的类名,即
When running java
you need to specify the full classname using periods not slashes, i.e.
java org.openmetadata.main.OmadUpdate
这篇关于从命令行运行 Java 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从命令行运行 Java 程序


基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01