简体   繁体   English

如何运行打包的Java文件

[英]How to run a packaged java file

Can anyone help me on how I have to run a java application which is in a package, from cmd? 谁能帮助我如何从cmd运行软件包中的Java应用程序? Please give me the necessary line that I have to type. 请给我我必须输入的必要行。

EDIT: (Copied a clarifying comment from one of the answers below) 编辑:(从下面的答案之一中复制了一个澄清的评论)

No I mean a normal Java aplication that belongs to a package, like: 不,我的意思是属于某个包的普通Java应用程序,例如:

package x; class SampleOnly{ }

How you compile (and run) that file. 您如何编译(和运行)该文件。

No i mean a normal java aplication that belongs 2 a package, like package x; class sampleOnly{ } 不,我的意思是一个普通的Java应用程序,它属于2一个包,例如package x; class sampleOnly{ } package x; class sampleOnly{ } hw u comple dat file package x; class sampleOnly{ } dat文件

This little "bash session" should explain it: 这个小小的“ bash会话”应该解释一下:

$ ls .                   # Current directory contains the "x" package
x

$ ls x                   # The "x" package contains a Sample.java file...
Sample.java

$ cat x/Sample.java      # ...which looks like this.
package x;
public class Sample {
    public static void main(String... args) {
        System.out.println("Hello from Sample class");
    }
}

$ javac x/Sample.java    # Use "/" as delimiter and
                         # include the ".java"-suffix when compiling.

$ java x.Sample          # Use "." as delimiter when running, and don't include
                         # the ".class" suffix.
Hello from Sample class

$ 

How to execute an arbitrary jar file, depends on how the jar file in question is pcakaged: 如何执行任意的jar文件,取决于有问题的jar文件如何打包:

If it's packaged with a manifest containing a Main-class declaraton, you simply do 如果它与包含Main-class声明的清单一起打包,则只需执行

java -jar program.jar

If it's a regular jar-file you need to know the main class. 如果它是常规的jar文件,则需要了解主类。 Let's say it's package.Main : 假设它是package.Main

java -cp program.jar package.Main

(I answered this (indirectly) here: How to convert Java program into jar? ) --> (我在这里(间接地)回答了: 如何将Java程序转换为jar? )->

A simple class is written as: 一个简单的类写为:

package com.demo.pack ; com.demo.pack ;

public class Demo { 公共课程演示 {

public static void main(String[] args) {
    System.out.println("Demo class from package : '"
            + Demo.class.getPackage().getName() + "'");

}

} }

This Demo class will be stored under these folders.. Folder sequence will be com/demo/pack . 该Demo类将存储在这些文件夹下。文件夹顺序为com / demo / pack Then the class will be found under pack folder. 然后,该类将在pack文件夹下找到。 Now open the command prompt.. I have stored that package in my 'D' drive. 现在打开命令提示符。我已将该软件包存储在“ D”驱动器中。 Type the following command. 键入以下命令。

D:>javac com/demo/pack/Demo.java -- press enter D:> javac com / demo / pack / Demo.java-按Enter

D:>java com.demo.pack.Demo -- press enter.. D:> java com.demo.pack.Demo-按Enter。

Done! 做完了!

By package you mean a .jar file? 打包是指.jar文件?

java [ options ] <class> [ arguments ... ]
java [ options ] -jar <file.jar> [ arguments ... ]
javaw [ options ] <class> [ arguments ... ]
javaw [ options ] -jar <file.jar> [ arguments ... ]

Running Java applications 运行Java应用程序

如果您正在谈论的是一个罐子,那么java -jar filename.jar

And not to forget setting the main class and optional classpath in META-INF/MANIFEST.MF 而且不要忘记在META-INF / MANIFEST.MF中设置主类和可选类路径

Main-Class: org.example.HelloWorld
Class-Path: greetings.jar

Have a look to the JAR specs at http://java.sun.com/javase/6/docs/technotes/guides/jar/jar.html http://java.sun.com/javase/6/docs/technotes/guides/jar/jar.html上查看JAR规范

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM