java package which performs a jar file class class

A environment:

1. linux

2. There are two java classes, A.java and B.java, all in the same directory

 

Second Source:

A.java

  1.  
    package com.abc;
  2.  
    public class A {
  3.  
    public static void test() {
  4.  
    System.out.println( "A:test()");
  5.  
    }
  6.  
    }

B.java

  1.  
    com.abc.A amount;
  2.  
    public class B {
  3.  
    public static void main(String[] argc) {
  4.  
    A a = new A();
  5.  
    a.test();
  6.  
    }
  7.  
    }

III. Compile and run

  1.  
    # Compiler
  2.  
    javac -d . A.java B.java
  3.  
     
  4.  
    #run
  5.  
    java B
  6.  
     
  7.  
    # Output
  8.  
    A: test()

IV. Not in the same directory

 To link ahead of each file (make_file (content): COM / filetool / util / A.java 
COM / filetool / main / B.java)

javac -source 1.7 -target 1.7 -d $APP_HOME/code/ecs/bin -encoding UTF-8 @$MAKE_FILE

 

 

two.

jar package class files, how to run dos command line?

If I do not run the main class jar package,
I want to run another jar file a class package.
Contents are as follows:
dist 
config.xml
SendMessage.jar
I want to run SendMessage.jar in Test.class file called from the command line, if realized, do not unpack.
Enter cmd
into the directory where you SendMessage.jar
execute java -cp SendMessage.jar Test.class

if Test.class have the package name, then the following

java -cp SendMessage.jar mypackagename.Test.class
 
 
 
three.
After the application labeled jar package through maven, jar can be performed by java -jar ***, main method will run the specified mainly in MANIFEST.MF main method specified; so if there are multiple main method of time how to run the specified main method which, by the following command to execute 

Java -classpath ****. JAR **** ****. className [args] 
**** herein. **** means is the package name; 

**** class name is className;. 

args parameter passed in the finger; 

sum up: there are two cases run the jar 

1> maven after the main packing method specified by the MANIFEST.MF 

Java-jar-Service--jar Mplus -with-dependencies.jar 
 if maven, you can add the following code to specify the main method pom.xml 

<Build> 
    <plugins> 
      <plugin> 
        <the groupId> org.apache.maven.plugins </ the groupId> 
        <the artifactId> Assembly-plugin-Maven </ the artifactId> 
        <Version> 2.3 </ Version> 
        <Configuration> 
          <appendAssemblyId>false</appendAssemblyId> 
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>io.test.main.MainOfDiskMount</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>assembly</goal>
            </goals>
 
          </ Execution> 
        </executions>
      </ plugin> 
    </ plugins> 
  </ Build> 
mainly inside mainfest this code block; package name + class name specified in the main method mainClass executed; maven package and specify the command, it can be seen that package the jar in the META-INF MANIFEST.MF add the contents of a content 



there is the main method we specified in the pom.xml; then packaged into a jar run time, if you want to add parameters, added directly behind it it; 

Java-Service-Mplus -jar JAR-with-AAA-BBB dependencies.jar 
 

2> if not specified in the MANIFEST.MF, jar in a plurality of main method, the run command specifies a particular main method 

java - cp mplus-service-jar-with -dependencies.jar com.smbea.dubbo.bin.Console aaa bbb 
 

and then the main method to add logic to print, the execution parameters situation 



is actually the AAA, bbb value as args parameters passed into it; 

summary: maven items packaged into the JAR, then run the main method, there are two cases, one is the main method specified in the MANIFEST.MF run; the other is designated to run from the command line mai A method of packet n; and can add transmission parameters corresponding to the main method;

  

Guess you like

Origin www.cnblogs.com/SunshineKimi/p/11306519.html