package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
javac -d directory javafilename
javac -d . Simple.java
Output:Welcome to package
import package.*;
import package.classname;
完全限定的名称。
//save by A.javapackage pack;
public class A{
public void msg(){
System.out.println("Hello");
}
}
//save by B.javapackage mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
//save by A.javapackage pack;
public class A{
public void msg(){
System.out.println("Hello");
}
}
//save by B.javapackage mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
//save by A.javapackage pack;
public class A{
public void msg(){
System.out.println("Hello");
}
}
//save by B.javapackage mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();
//using fully qualified name obj.msg();
}
}
Output:Hello
package com.lidihuo.core;
class Simple{
public static void main(String args[]){
System.out.println("Hello subpackage");
}
}
要编译: javac -d . Simple.java |
要运行: java com.lidihuo.core.Simple |
Output:Hello subpackage
//save as Simple.javapackage mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
Output:Welcome to package
临时 通过在命令提示符下设置类路径 通过-classpath切换
永久 通过在环境变量中设置类路径 通过创建包含所有类文件的jar文件,并将jar文件复制到jre/lib/ext文件夹中。
//save as C.java otherwise Compilte Time Errorclass A{
}
class B{
}
public class C{
}
//save as A.javapackage lidihuo;
public class A{
}
//save as B.javapackage lidihuo;
public class B{
}