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 程序
基础教程推荐
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- JPA惰性列表上的流 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 double 转换为 Int,向下舍入 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
