码迷,mamicode.com
首页 > 编程语言 > 详细

将本地Java程序部署到linux服务器并执行

时间:2014-05-06 23:43:26      阅读:488      评论:0      收藏:0      [点我收藏+]

标签:style   class   code   java   tar   ext   

需求:将本地Java程序部署到linux服务器并执行,首先要将本地Java程序打成jar包,然后拷贝到服务器后运行java -jar xx.jar
问题:该程序本地运行正常,服务端运行报错找不到第三方包中的类
原因:本地打jar包时有错,未能正确引入第三方包
解决:本地通过fat jar插件来打包,方法如下


在Eclipse 下 java 应用程序打包 Mainfest.mf 格式

首先保证程序在Eclipse下的正常执行,然后方法如下:

( 写在最前面 做好的Eclipse打包 还是使用插件 Fat jar)
下载地址 :http://sourceforge.net/project/showfiles.php?group_id=115990&package_id=125924
使用说明:
Fat Jar Eclipse Plug-In Tutorial


Step 1: Create a new Java Project "demolib"

Create a new Java Project named "demolib".
Add the Class "demolib.DemoLib.java" containing the following code:

package demolib;

public class DemoLib {
public static void sayHello() {
System.out.println("Hello");
}
}

The Project should look something like this:

bubuko.com,布布扣


Step 2: Create a jar file using Fat Jar Plug-In

In the "Package-Explorer" (not the "Resource-View") right click on the project "demolib".
Select "+ Build Fat Jar".

bubuko.com,布布扣

A Configuration Dialog appears. Just press "Finish".

bubuko.com,布布扣

The File "demolib_fat.jar" has been created in the project root directory.

bubuko.com,布布扣


Step 3: Create a new Java-Project "demorun"

Create a new Java Project named "demorun".
In the project properties add the Library "demolib/demolib_fat.jar" to the Java Build Path":

bubuko.com,布布扣


Step 4: Create Main Class

Add the Class "demorun.DemoRunMain.java" containing the following code:

package demorun;

import demolib.DemoLib;

public class DemoRunMain {
public static void main(String[] args) {
DemoLib.sayHello();
}
}

The Project should look something like this:

bubuko.com,布布扣


Step 5: Start the Build Fat Jar Dialog

Start the Export Wizard from the File-Menu ("File" -> "Export").
Select "+ Fat Jar Exporter" and click "next >".

bubuko.com,布布扣

Select the project "demorun" and click "next >".

bubuko.com,布布扣

A Configuration-Dialog appears showing the current Settings.

bubuko.com,布布扣


Step 6: Select the Main Class

The Main Class - the one containing the static methode main - must be defined in the jar.
Click on the "Browse..." Button on the right side behind the Main-Class Edit field.

bubuko.com,布布扣

Select "DemoRunMain" and click the "OK" Button.
The FullyQualifiedName "demorun.DemoRunMain" is now set for "Main-Class".


Step7: Finish

Save the current Settings by clicking on the "Finish" Button.

bubuko.com,布布扣

The File "demorun_fat.jar" has been created in the project root directory.
In addition the file ".fatjar" storing the configuration settings has been created in the project root directory

The created jar file contains all classes from all referenced jar files (demolib_fat.jar) and the project classes.
This file can be executed anywhere, no classpath has to be set, because all necessary libraries
are extracted inside the "Fat Jar":

> java -jar demorun_fat.jar
Hello

将本地Java程序部署到linux服务器并执行,布布扣,bubuko.com

将本地Java程序部署到linux服务器并执行

标签:style   class   code   java   tar   ext   

原文地址:http://blog.csdn.net/berber78/article/details/25146753

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!