编程经验:include package java build in linux

第一种:包含package编译运行

// MySocketServer.java文件

 

package canyou.cys.datapool;

import java.io.*;

import java.net.*;

 

/**

 *@author Administrator

 *

 */

//socket 服务端

public class MySocketServer

{

       /**

        * @param args

       */

       public static void main(String[] args)

       {

              // TODO Auto-generated method stub

    }   

}

//------------------------------------------------------------------------------

root@bt:/java2# pwd

/java2

root@bt:/java2# ls

MySocketServer.java

root@bt:/java2# javac -d . MySocketServer.java

root@bt:/java2# ls

canyou  MySocketServer.java

root@bt:/java2# cd /java2/canyou/cys/datapool/

root@bt:/java2/canyou/cys/datapool# ls

MySocketServer.class

root@bt:/java2/canyou/cys/datapool# java MySocketServer

Exception in thread "main" java.lang.NoClassDefFoundError: MySocketServer (wrong name: canyou/cys/datapool/MySocketServer)

        at java.lang.ClassLoader.defineClass1(Native Method)

        at java.lang.ClassLoader.defineClass(ClassLoader.java:621)

        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)

        at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)

        at java.net.URLClassLoader.access$000(URLClassLoader.java:56)

        at java.net.URLClassLoader$1.run(URLClassLoader.java:195)

        at java.security.AccessController.doPrivileged(Native Method)

        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)

        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)

        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)

Could not find the main class: MySocketServer.  Program will exit.

root@bt:/java2/canyou/cys/datapool# cd /java2/

root@bt:/java2# ls

canyou  MySocketServer.java

root@bt:/java2# java canyou.cys.datapool.MySocketServer

Listen port :18888 ...

 

 

第二种:引用其它package编译运行

// 修改MySocketServer.java文件

 

package canyou.cys.datapool;

import java.io.*;

import java.net.*;

import com.cys.data.*;

 

/**

 *@author Administrator

 *

 */

//socket 服务端

public class MySocketServer

{

       /**

        * @param args

       */

       public static void main(String[] args)

       {

              // TODO Auto-generated method stub

        MyTest t = new MyTest();  //调用MyTest.javaTestPrintl()方法

        t.TestPrintln();

    }

}

//MyTest.java文件

package com.cys.data;

import java.io.*;

import java.net.*;

 

/**

 *@author Administrator

 *

 */

public class MyTest

{

       /**

        * @param args

       */

       public static void TestPrintln ()

       {

              // TODO Auto-generated method stub

         try

         {

             System.out.println("Creating new TestPrintln() in main");

             

         }

         catch(Exception e)

         {

                 e.printStackTrace();

         }

    }

}

//------------------------------------------------------------------------------

root@bt:/java2# javac -d . MyTest.java

root@bt:/java2# javac -d . MySocketServer.java

root@bt:/java2# java canyou.cys.datapool.MySocketServer

Creating new TestPrintln() in main

Listen port :18888 ...

 

第三种:引用外部.jar方法编译运行

 

root@bt:/java2# javac -d . MyTest.java  //编译外部文件

root@bt:/java2# jar cvf com.jar com/*   //外部文件打包成jar

标明清单(manifest)

增加com/cys/(读入= 0) (写出= 0)(存储了 0%)

增加com/cys/data/(读入= 0) (写出= 0)(存储了 0%)

增加:com/cys/data/MyTest.class(读入= 516) (写出= 351)(压缩了 31%)

root@bt:/java2# cat /etc/profile

CLASSPATH=.:/home/jdk1.5.0_22/jre/lib/rt.jar:/java2/com.jar  

root@bt:/java2# logout

root@bt:/java2# javac -d . MySocketServer.java

root@bt:/java2# java canyou.cys.datapool.MySocketServer

Creating new TestPrintln() in main

Listen port :18888 ...

 

CLASSPATH linux是以”:”冒号分开的。修改完记住要注销喔!

 

 

第四种:用find命令批量引用外部.jar方法编译运行

 

root@bt:/java2/lib# ls

a.jar  b.jar  CanYou  d.jar  e.jar

root@bt:/java2/lib#   

 

Linux下可以通过这样的方法把/java2/lib目录下的所有jar文件添加到CLASSPATH'

root@bt:/java2# find /java2/lib -name *.jar|tr '\n' ':'

/java2/lib/a.jar:/java2/lib/e.jar:/java2/lib/d.jar:/java2/lib/b.jar:/java2/lib/CanYou/com.jar:

 

同样的方法,可以把/java2/lib他下面的目录以及目录下面的子目录和更下层目录也添加到CLASSPATH里面则通过以下方法

 

root@bt:/java2# find /java2/lib -type d|tr '\n' ':'

/java2/lib:/java2/lib/CanYou:

  

root@bt:/java2# vim /etc/profile

CLASSPATH=.:/home/jdk1.5.0_22/jre/lib/rt.jar:`find /java2/lib -name *.jar|tr '\n' ':'`

root@bt:/java2# logout

root@bt:/java2# set    //查看下环境变量

CLASSPATH=.:/home/jdk1.5.0_22/jre/lib/rt.jar:/java2/lib/a.jar:/java2/lib/e.jar:/java2/lib/d.jar:/java2/lib/b.jar:/java2/lib/CanYou/com.jar:

 

root@bt:/java2# javac -d . MySocketServer.java

root@bt:/java2# java canyou.cys.datapool.MySocketServer

Creating new TestPrintln() in main

Listen port :18888 ...

 

第五种:java jar  ***.jar文件执行

 

打包好我们jar文件

root@bt:/java2# javac -d . MySocketServer.java

root@bt:/java2# jar cvf canyou.jar canyou/*   //打包成jar

root@bt:/java2# ls

canyou.jar  lib           //lib包含我们canyou.jar里面class需要调用的第三方jar文件

root@bt:/java2#

//制作MANIFEST.MF文件

..

root@bt:/java2# java -jar canyou.jar

Creating new TestPrintln() in main

Listen port :18888 ...

 

winrar压缩软件打开我们的canyou.jar,编辑META-INF/MANIFEST.MF文件

Manifest-Version: 1.0

Created-By: 1.5.0_22 (Sun Microsystems Inc.)

Class-Path: lib/CanYou/com.jar

Main-Class: canyou.cys.datapool.MySocketServer

 

注意最后一行需要一个回车符,每个冒号”:” 有一个空格。 Class-Path指定我们需要的第三方jarMain-Class指定我们当前需要运行的.jarmain函数所在的类。

posted @ 2013-06-13 14:37  信息无障碍  阅读(165)  评论(0编辑  收藏  举报