当前页面: 开发资料首页 → J2ME 专题 → 如何让JAR文件成为试玩版?
摘要: 如何让JAR文件成为试玩版?
Jar文件生成器类
*
*
描 述:这是JarCreator工程的Jar文件生成器类,将完成以下几个任务:
*
1、重新打包Jar文件;
*
* @version 1.00, 08/21/06
* @author 窦海宁, chong0660@sina.com
*/
public class JarCreator {
/**
*
创建Jar文件
*
* @param fromJar 源Jar文件
* @param toJar 目标Jar文件
* @param insertClassNames 插入类名称数组
* @param insertClassPaths 插入类路径数组
* @param mainClassName 带完整包名的主类名称
*
* @return Jar文件创建状态
*/
public int create(String fromJar , String toJar , String[] insertClassNames , String[] insertClassPaths , String mainClassName) {
Manifest manifest = null;
JarInputStream jarIn = null;
JarOutputStream jarOut = null;
//创建Jar文件输入流并获取Manifest文件信息
try {
jarIn = new JarInputStream(new FileInputStream(fromJar));
manifest = jarIn.getManifest();
if (manifest == null) {
manifest = new Manifest();
}
//修改Manifest文件
Attributes attribute = manifest.getMainAttributes();
String midletName = null;
StringBuffer midletNameSb = null;
attribute.putValue("Manifest-Version" , "1.0");
midletName = attribute.getValue("MIDlet-1");
if (midletName != null) {
midletNameSb = new StringBuffer();
midletNameSb.append(midletName.substring(0 , midletName.lastIndexOf(",") + 1));
midletNameSb.append(mainClassName);
midletName = midletName.substring(midletName.lastIndexOf(",") + 1);
attribute.putValue("Joyes-Start" , midletName);
}
//用Manifest对象创建新的Jar文件
jarOut = new JarOutputStream(new FileOutputStream(toJar) , manifest);
//循环插入指定文件
insertJar(jarOut , insertClassNames , insertClassPaths);
//循环读取源Jar文件数据至目标Jar文件中
copyJar(jarIn , jarOut);
//清空缓冲区
jarOut.flush();
//返回创建成功标识
return 0;
} catch (FileNotFoundException ex) {
//返回IO错误标识
return -1;
} catch (IOException ex) {
//返回IO错误标识
return -2;
} finally {
try {
if (jarOut != null) {
jarOut.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
try {
if (jarIn != null) {
jarIn.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
*
复制Jar文件
*
* @param fromJar 源Jar文件
* @param toJar 目标Jar文件
*
* @throws IOException
*/
private void copyJar(JarInputStream jarIn , JarOutputStream jarOut) throws IOException {
JarEntry entry = null;
while ((entry = jarIn.getNextJarEntry()) != null) {
jarOut.putNextEntry(entry);
int read = 0;
byte[] bytes = new byte[4096];
while ((read = jarIn.read(bytes)) != -1) {
jarOut.write(bytes , 0 , read);
}
jarOut.closeEntry();
}
}
/**
*
插入指定文件至Jar文件中
*
* @param jarOut 源Jar文件输出流
* @param insertClassNames 插入类名称数组
* @param insertClassPaths 插入类路径数组
*
* @throws FileNotFoundException , IOException
*/
private void insertJar(JarOutputStream jarOut , String[] insertClassNames , String[] insertClassPaths) throws FileNotFoundException , IOException {
for (int i = 0 ; i < insertClassNames.length ; i++) {
File file = new File(insertClassPaths[i]);
FileInputStream fs = new FileInputStream(file);
JarEntry jarEntry = new JarEntry(insertClassNames[i]);
jarOut.putNextEntry(jarEntry);
//写入输出流
int read = 0;
byte[] bytes = new byte[4096];
while ((read = fs.read(bytes)) != -1) {
jarOut.write(bytes , 0 , read);
}
jarOut.closeEntry();
}
}
}