简体   繁体   English

在Windows命令行上的jar文件的子包中运行类

[英]Run a class in a sub package in a jar file on Windows command line

I exported a jar file from eclipse and in the jar file are various packages and sub packages with java class files. 我从eclipse中导出了一个jar文件,在jar文件中有Java类文件的各种包和子包。

I'd like to run one of these nested class files on the command line in Windows. 我想在Windows的命令行中运行这些嵌套类文件之一。

The class has a main and I am using the following to try to run it, 该类有一个main,我正在使用以下代码尝试运行它,

java -classpath .;./example.jar example

Note that example is the name of the class as well as the jar. 请注意,示例是类以及jar的名称。

I've also tried to spell out the full path of the class 我也曾尝试阐明课程的完整路径

java -classpath .;./example.jar the.whole.path.example

How can I run the example class? 如何运行示例类?

EDIT: 编辑:

OK this is kind of stupid, the full path was incorrect. 好的,这有点愚蠢,完整路径不正确。 I checked this over numerous times yet it was only when I came back to it that I noticed the error. 我检查了无数次,但是直到我回到它时,我才注意到错误。

Without seeing any of your code or any output (hint, hint) it's hard to say what's happening, but this works for me: 没有看到您的任何代码或任何输出(提示,提示),很难说是怎么回事,但这对我有用:

$ cat x/y/z/A.java
package x.y.z;

public class A
{
    public static void main(String args[]) throws Exception
    {
        System.out.println("here in A");
    }
}
$ javac x/y/z/A.java
$ jar cvf a.jar x/y/z/A.class
added manifest
adding: x/y/z/A.class(in = 459) (out= 311)(deflated 32%)
$ java -classpath a.jar x.y.z.A
here in A

And in case the poster or someone reading this in the future isn't familiar with Unix, the lines starting with $ are commands I type into the shell and everything else is output from those commands. 并且如果发帖人或以后会读到此书的人对Unix不熟悉,以$开头的行是我在shell中键入的命令,其他所有内容都从这些命令输出。 Eclipse will take care of the first three for you, then the final java -classpath a.jar xyzA is the command to execute the main method in the xyzA class. Eclipse将为您处理前三个问题,然后最后一个java -classpath a.jar xyzA是执行xyzA类中的main方法的命令。

Just running java -cp example.jar the.whole.path.example should do the trick. 仅运行java -cp example.jar the.whole.path.example应该可以解决问题。 If not, then something with your JAR file is wrong. 如果不是,则JAR文件中的内容有误。 The class name must be fully qualified (with package name) and the specified class must have a correct main method: 类名必须是完全限定的(带有包名),并且指定的类必须具有正确的main方法:

public static void main(String[] args) {

}

Check if your assumptions are correct. 检查您的假设是否正确。 Show the structure of your jar and compare it to what you try to run: 显示罐子的结构并将其与您尝试运行的罐子进行比较:

jar tf example.jar

If the jar contains a lot of entries you might want to grep / find the relevant class: 如果罐子中包含很多条目,则可能要grep /查找相关的类:

jar tf  example.jar | find /i "Example"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Jar 文件主类在从命令行运行时运行,而不是从 Windows 批处理文件运行 - Java Jar file main class run when runs from command line, but not from Windows batch file 在Windows上尝试使用classpath(用于其他lib)从命令行运行jar文件会引发“找不到或加载主类” - Trying to run jar file from command line using classpath(for other libs) raises “Could not find or load main class”, on Windows 在Windows(Classpath)的命令行中运行jar - Run jar in command line on Windows (Classpath) java-如何在Windows命令行中运行JAR文件,该命令行的运行时参数包含“&lt;”和“&gt;”字符 - java- how to run JAR file in Windows command line which has run time arguments containing “<” and “>” chars 在Windows命令行中将Jar文件添加到Buildpath - Add Jar File to Buildpath in Windows Command Line 无法从命令行运行jar文件 - Unable to run jar file from command line 在命令行中运行jar文件(由ant生成) - run a jar file (generated by ant) in command line 从 unix 命令行运行 jar 文件 - Run a jar file from unix command line 如何从Eclipse编译的命令行运行Java类文件?外部Jar问题 - How to Run a Java Class File from the Command Line that was Compiled by Eclipse? External Jar problems 如何在 windows 的命令行中运行此 java class? - How to run this java class in command line in windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM