java基本例子

时间:2022-10-31 18:36:55

文件结构

D:\jp\jarDemo\IAmMainClass.java

import iAmPackage.*;

public class IAmMainClass
{
public static void main(String[] args)
{
System.out.println("I am MainClass!"); //这个是直接调用的方法,必须是静态的。
iAmPackage.IAmALib.IAmStaticFunction("ha ha"); IAmALib libObj = new IAmALib();
//这个是实例后的对象方法,不需要一定是静态的。
libObj.IAmGenaralFunction("gu gu");
} }

D:\jp\jarDemo\iAmPackage\IAmALib.java

package iAmPackage;

public class IAmALib
{
public static void IAmStaticFunction(String var)
{
System.out.println("I am AmStatic function!" + var);
} public void IAmGenaralFunction(String var)
{
System.out.println("I am Genaral function!" + var);
}
}

D:\jp\jarDemo\MANIFEST.MF

Manifest-Version: 1.0
Created-By: 1.8.0_60 (Oracle Corporation)
Main-Class: IAmMainClass
 //注意最后必须是一行空行

1.编译

D:\jp\jarDemo>javac IAmMainClass.java

D:\jp\jarDemo\iAmPackage>javac IAmALib.java

2.打包

D:\jp\jarDemo>jar cvfm jarDemo.jar ./MANIFEST.MF ./IAmMainClass.class ./iAmPackage/IAmALib.class

3.执行

D:\jp\jarDemo>java -classpath . IAmMainClass

I am MainClass!
I am AmStatic function!ha ha
I am Genaral function!gu gu