- 浏览: 272346 次
- 性别:
- 来自: 山东
最新评论
-
xchz:
没想到做的这么完善
状态模式-实现屏幕截取程序 -
songwangchu:
不错啊。哈哈哈
Reactor模式,或者叫反应器模式 -
yuanliangding:
很简洁易懂。怎么没有系列文章
Reactor模式,或者叫反应器模式 -
过冷水:
讲的不错
Reactor模式,或者叫反应器模式 -
Hero_z:
讲的非常通俗易懂,108个赞!!!
Reactor模式,或者叫反应器模式
除了使用JDK自带的jar命令对生成的class文件进行打包,还可以在程序中使用java.util.jar包的类,来实现对class文件的打包。网上找到的代码,写得不错,这里记录一下。
========================================================
2009-12-12日更新,原来代码居然了一对括弧,大意啦
另增加了对Manifest文件的支持,在网上找了一下没有找到,找了一下API还真的找到了,一同更新一下,这样打包时就可以指定入口的主类了。
========================================================
2009-12-12日更新,原来代码居然了一对括弧,大意啦
另增加了对Manifest文件的支持,在网上找了一下没有找到,找了一下API还真的找到了,一同更新一下,这样打包时就可以指定入口的主类了。
package test; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarInputStream; import java.util.jar.JarOutputStream; import java.util.jar.Manifest; /** * Provides utility services for jarring and unjarring files and directories. * Note that a given instance of JarHelper is not threadsafe with respect to * multiple jar operations. * * @author Patrick Calahan <pcal@bea.com> */ public class JarHelper { // ======================================================================== // Constants private static final int BUFFER_SIZE = 2156; private static String MAIN_CLASS; // ======================================================================== // Variables private byte[] mBuffer = new byte[BUFFER_SIZE]; private int mByteCount = 0; private boolean mVerbose = false; private String mDestJarName = ""; // ======================================================================== // Constructor /** * Instantiates a new JarHelper. */ public JarHelper() { } // ======================================================================== // Public methods /** * Jars a given directory or single file into a JarOutputStream. */ public void jarDir(File dirOrFile2Jar, File destJar) throws IOException { if (dirOrFile2Jar == null || destJar == null) throw new IllegalArgumentException(); mDestJarName = destJar.getCanonicalPath(); FileOutputStream fout = new FileOutputStream(destJar); JarOutputStream jout; if (MAIN_CLASS != null) { Manifest manifest = new Manifest(); Attributes attrs = manifest.getMainAttributes(); attrs.putValue("Manifest-Version", "1.0"); attrs.putValue("Class-Path", "."); attrs.putValue("Main-Class", MAIN_CLASS); /** * Manifest-Version: 1.0 * Class-Path: . * Main-Class: test.JarHelper */ jout = new JarOutputStream(fout, manifest); } else { jout = new JarOutputStream(fout); } // jout.setLevel(0); try { jarDir(dirOrFile2Jar, jout, null); } catch (IOException ioe) { throw ioe; } finally { jout.close(); fout.close(); } } /** * Unjars a given jar file into a given directory. */ public void unjarDir(File jarFile, File destDir) throws IOException { BufferedOutputStream dest = null; FileInputStream fis = new FileInputStream(jarFile); unjar(fis, destDir); } /** * Given an InputStream on a jar file, unjars the contents into the given * directory. */ public void unjar(InputStream in, File destDir) throws IOException { BufferedOutputStream dest = null; JarInputStream jis = new JarInputStream(in); JarEntry entry; while ((entry = jis.getNextJarEntry()) != null) { if (entry.isDirectory()) { File dir = new File(destDir, entry.getName()); dir.mkdir(); if (entry.getTime() != -1) dir.setLastModified(entry.getTime()); continue; } int count; byte data[] = new byte[BUFFER_SIZE]; File destFile = new File(destDir, entry.getName()); if (mVerbose) System.out.println("unjarring " + destFile + " from " + entry.getName()); FileOutputStream fos = new FileOutputStream(destFile); dest = new BufferedOutputStream(fos, BUFFER_SIZE); while ((count = jis.read(data, 0, BUFFER_SIZE)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); if (entry.getTime() != -1) destFile.setLastModified(entry.getTime()); } jis.close(); } public void setVerbose(boolean b) { mVerbose = b; } // ======================================================================== // Private methods private static final char SEP = '/'; /** * Recursively jars up the given path under the given directory. */ private void jarDir(File dirOrFile2jar, JarOutputStream jos, String path) throws IOException { if (mVerbose) System.out.println("checking " + dirOrFile2jar); if (dirOrFile2jar.isDirectory()) { String[] dirList = dirOrFile2jar.list(); String subPath = (path == null) ? "" : (path + dirOrFile2jar.getName() + SEP); if (path != null) { JarEntry je = new JarEntry(subPath); je.setTime(dirOrFile2jar.lastModified()); jos.putNextEntry(je); jos.flush(); jos.closeEntry(); } for (int i = 0; i < dirList.length; i++) { File f = new File(dirOrFile2jar, dirList[i]); jarDir(f, jos, subPath); } } else { if (dirOrFile2jar.getCanonicalPath().equals(mDestJarName)) { if (mVerbose) System.out.println("skipping " + dirOrFile2jar.getPath()); return; } if (mVerbose) System.out.println("adding " + dirOrFile2jar.getPath()); FileInputStream fis = new FileInputStream(dirOrFile2jar); try { JarEntry entry = new JarEntry(path + dirOrFile2jar.getName()); entry.setTime(dirOrFile2jar.lastModified()); jos.putNextEntry(entry); while ((mByteCount = fis.read(mBuffer)) != -1) { jos.write(mBuffer, 0, mByteCount); if (mVerbose) System.out.println("wrote " + mByteCount + " bytes"); } jos.flush(); jos.closeEntry(); } catch (IOException ioe) { throw ioe; } finally { fis.close(); } } } // for debugging public static void main(String[] args) throws IOException { if (args.length < 2) { System.err.println("Usage: JarHelper jarname.jar directory [MainClass]"); return; } if (args.length == 3) { MAIN_CLASS = args[2]; } JarHelper jarHelper = new JarHelper(); jarHelper.mVerbose = true; File destJar = new File(args[0]); File dirOrFile2Jar = new File(args[1]); jarHelper.jarDir(dirOrFile2Jar, destJar); } }
评论
3 楼
daimojingdeyu
2012-02-14
yaoandw 写道
拿去用了,thank you
2 楼
yaoandw
2012-02-14
拿去用了,thank you
1 楼
llp20_2000
2008-11-14
变量名:dirOrFile2Jar .我以前也想写个,但是JAR视乎不能更新,每次向里面加entry的时候都要从头开始重新建立一个新jar,觉得还是用命令方便,就罢了,这个又让我想起了呵呵.谢谢博主
发表评论
-
转:idea点用C盘空间解决办法
2015-03-30 17:02 1463原文:http://my.oschina.net/ulyn ... -
转:Java安全模型
2015-02-26 13:50 792原文地址:http://www.ibm.com/develo ... -
序列化对象大小计算
2013-01-21 16:45 0import java.io.ByteArrayOutput ... -
wait死了,今天真是郁闷
2012-09-13 22:21 1009wait should always be in syn ... -
获取所有淘金币全额兑换商品
2012-08-08 16:36 1735找淘金币全额兑换的商品是不是很麻烦,点来点去每个类目找一下,等 ... -
gc 日志格式
2011-03-04 14:58 0Here's an example GC log file w ... -
使用Oauth向新浪微博发消息
2011-02-13 00:16 4749最近看了一下新浪围脖 ... -
classloader
2011-02-12 16:57 0/** * classloader single * ... -
mail
2011-02-12 16:28 0package org.mail.core; impor ... -
Mina原理草图及注释
2011-01-18 23:00 5926今天先画一个草图备忘,明天再注释一下。 上图 ... -
Reactor模式,或者叫反应器模式
2010-11-29 22:29 69397Reactor这个词译成汉语还真没有什么合适的,很多地方叫反应 ... -
eclipse“随变”,随机变换eclipse启动界面
2010-06-02 20:17 4315对eclipse的启动界面审美疲劳了,手贱,想换掉它,趁老婆不 ... -
java编译时生成调试信息选项详解(javac -g)
2010-05-30 02:31 6268引子 先说一下为什么 ... -
文件Copy,什么方式才最快呀~~
2010-05-17 23:31 1519闲逛CSDN,发现有人找文件Copy的方法,顺手解答了一下,有 ... -
Eclipse调试常用技巧
2010-04-06 01:43 25727本文写给那些像几年前的我一样刚刚走出校门,及一些未使用过这些高 ... -
log4j真的比JDK logger快吗?
2009-09-20 23:58 4213这里不想比较这两个日 ... -
String.format函数使用方法介绍
2009-08-09 10:52 2042转自:http://blog.csdn.net/andycpp ... -
在程序中实现对java源文件编译的3种方法
2009-07-18 18:34 2702一般情况下对java源文件的编译均是在代码完成后使用javac ... -
Java读带有BOM的UTF-8文件乱码原因及解决方法
2009-05-28 01:31 20273最近在处理文件时发现了同样类型的文件使用的编码可能是不同的。所 ... -
Java正则与栈溢出
2009-05-13 00:09 4825使用RegexBuddy测试正确的 ...
相关推荐
java 代码打包成exe
Eclipse打包带mysql的java程序
NULL 博文链接:https://zhaoshunxin.iteye.com/blog/1239723
核心提示:首先,将编译好的程序打包成jar文件,然后做出exe,这样代码就不可见了;但是exe文件在没有安装jre的电脑上不能运行,如果要求客户再去安装jre 设置环境变量 就不雅了。我们需要将jre打包。 这几步操作...
简介: 资源中打包了100多个经典的java小程序,并附四个java小型项目的源码,供打基础、求提高的java程序爱好者使用。 四个小型项目: java五子棋 JAVA聊天 JAVA扫雷 JAVA俄罗斯方块
java源代码生成exe可执行程序打包.pdf
使用maven将Java程序打包成exe文件并制作成Windows系统服务(六)之使用allatori对Java代码混淆
该软件是本人利用java GUI轻量级组件,以及jxl.jar读取excel表实现的随机点名程序,程序已经打包,可直接运行。
博文链接:https://pipe.iteye.com/blog/143334
使用maven将Java程序打包成exe文件并制作成Windows系统服务之springboot打包(一)项目代码
java编写的浏览器 包含源代码和打包程序 极具参考价值
java课程设计 中国象棋游戏 内含源代码 打包发布
本程序可以画各种函数曲线,功能有: 1.新建、打开、保存画图 2.多种方式输入函数作图 3.可设置画笔颜色、大小 4.可以放大、缩小 5.可以移动坐标和图像 6.实时显示鼠标经过的坐标 jar使用:第一步:配置好java的JDK ...
java贪吃蛇小游戏代码java小游戏源码.zip TankWar1.7.zip TencentQQ.rar WuyouTetris.zip 一份不错的Java版QQ源码.zip 企业人事管理系统 JAVA.zip 坦克大战3.0.rar 基于JAVA学生成绩管理系统设计软件源码.zip 基于...
把Java程序打包成EXE可执行文件的一款很好用的软件
java程序设计非常实用的小程序源码,这是我学习java以来开发的几个小练习程序,有五子棋、黑白棋、揣数字、放大镜、连连看、计数器、计算器、等等,非常优秀充实的资源,不容错过
Java程序设计讲义课后答案及源代码打包
核心提示:首先,将编译好的程序打包成jar文件,然后做出exe,这样代码就不可见了;但是exe文件在没有安装jre的电脑上不能运行,如果要求客户再去安装jre 设置环境变量 就不雅了。我们需要将jre打包。 这几步操作...
教你使用exe4j软件把java源代码打包成exe文件!
六个java小程序,已经经过打包处理,请放心使用