困扰所有初学者的问题,如何发布java程序,希望大家积极参加,共同解决这个问题,谢谢! 运行环境是JRE1.4.1?应该是j2sdk吧 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我遇到过这样的问题,完全一样如果用winRar压缩则不正常用jar命令就正常了。偶觉得很奇怪。 这个问题,应该是没有问题的因为1.4.1是肯定兼容1.3.1的,在低版本中的,在高版本中没有问题的除非,另外机器的版本低于你的版本按照你说的,不可能,或者是你的1.4.1,只是按照jvm了吗? 怎么没有一个人说到点子上呢我问的是jar文件为什么不能运行? 自解压文件 我们先来了解一下自解压文件,在window下可以用自解压制作工具如winzip self-Extractor来制作自解压文件,这些工具会把一个zip文件与解压程序打包在一起而产生一个新的可执行文件。然后只要运行这个可执行文件,就可以把zip文件中的内容解开。那为什么要创建自解压jar文件呢,创建成自解压zip文件不就好了?我们应该注意到自解压jar文件可以在任意支持java的平台上解开并执行,例如,可以在linux下执行。创建jar自解压文件很简单,只需要一个特殊的JAR manifest文件、一个基于java的解压程序(这个程序是原作者写的)、包含基本文件的jar 或者zip文件以及任何jsdk的jar应用程序 manifest文件 要生成可执行jar文件,需要在META-INF 目录下的manifest文件,文件名为:MANIFEST.MF ,但在我们这里我们只需要在文件中指定在这个基于java 的解压程序中包含main()的类的名称:Main-Class: ZipSelfExtractor 我们已经把一个叫做jarmanifest的文件加入到这个技巧的源程序包中。 解压程序 你可以用各种方法来实现这个解压程序,在我们这里使用了一个简单直接的办法。首先,解压程序判断这个自解压jar文件的名称,有了这个文件名,解压程序使用解压标准,把文件解开。具体的可以查看在源码包中的ZipSelfExtractor.java文件。 值得一提的是这里用了一个很巧妙的办法获取jar文件的文件名,虽然在命令行中出现的这个文件的名字,但它并没有作为参数传入类的main()中,因此,这里使用了以下的代码来获取文件名: private String getJarFileName () { myClassName = this.getClass().getName() + ".class"; URL urlJar = this.getClass().getClassLoader().getSystemResource(myClassName); String urlStr = urlJar.toString(); int from = "jar:file:".length(); int to = urlStr.indexOf("!/"); return urlStr.substring(from, to); } 请注意:getSystemResource() 中使用了myClassName而不是ZipSelfExtractor.class作参数,这使得我们可以更改加压程序的名字而不需要修改代码。 接下来,我们来分析获得这个jar文件的名字。首先,可以获取指向包含正在运行类的文件,urlStr = urlJar.toString();有了这个url,把jar文件名去掉,剩下的就是我们想要的,下面是这个url的格式: jar:file:/home/test/zipper.jar!/ZipSelfExtractor.class 有了文件名,就可以开始解压,详细的解压算法请大家自己看源码。 为了可以更方便实用,程序使用了图形界面,程序中使用了JFileChooser类可以选择要解压的目标目录。 最后程序还确保不把这两个文件:manifest文件和extractor's .class(在我们这里是ZipSelfExtractor.class)文件也解出来,程序是用来解开原始的jar的内容,而这两个文件并属于jar原始内容。 打包jar文件 有了manifest文件与解压程序,我们就可以创建自解压jar文件了,以下是一个例子: 1.创建一个zip文件Myzip.zip 2.下载zipper.jar 3.把文件解到当前目录,准备制作自解压jar文件 java -jar zipper.jar 4.把zipper.class拷贝成 ZipSelfExtractor.class 5.把 myzip.zip 重命名为 myzip.jar 6.把myzip.jar中的内容替换为jarmanifest和ZipSelfExtractor.class这两个文件 jar uvfm myzip.jar jarmanifest ZipSelfExtractor.class 7.执行java -jar myzip.jar就可以看到效果了,试试看 后记 一个自解压的jar文件能够很好的跨平台使用,自解压jar文件创建简单,只需要有jre1.2或或者更新的版本就可以实现了。 附自解压程序的源代码: /* ZipSelfExtractor.java */ /* Author: Z.S. Jin Updates: John D. Mitchell */ import java.io.*; import java.net.*; import javax.swing.*; import java.util.zip.*; import java.util.*; import java.text.*; public class ZipSelfExtractor extends JFrame { private String myClassName; static String MANIFEST = "META-INF/MANIFEST.MF"; public static void main(String[] args) { ZipSelfExtractor zse = new ZipSelfExtractor(); String jarFileName = zse.getJarFileName(); zse.extract(jarFileName); System.exit(0); } ZipSelfExtractor() { } private String getJarFileName() { myClassName = this.getClass().getName() + ".class"; URL urlJar = this.getClass().getClassLoader().getSystemResource(myClassName); String urlStr = urlJar.toString(); int from = "jar:file:".length(); int to = urlStr.indexOf("!/"); return urlStr.substring(from, to); } public void extract(String zipfile) { File currentArchive = new File(zipfile); JFileChooser fc = new JFileChooser(); fc.setCurrentDirectory(new File(".")); fc.setDialogType(JFileChooser.OPEN_DIALOG); fc.setDialogTitle("Select destination directory for extracting " + currentArchive.getName()); fc.setMultiSelectionEnabled(false); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if (fc.showDialog(ZipSelfExtractor.this, "Select") != JFileChooser.APPROVE_OPTION) { return; //only when user select valid dir, it can return approve_option } File outputDir = fc.getSelectedFile(); byte[] buf = new byte[1024]; SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yyyy hh:mma",Locale.getDefault()); ProgressMonitor pm = null; boolean overwrite = false; ZipFile zf = null; FileOutputStream out = null; InputStream in = null; try { zf = new ZipFile(currentArchive); int size = zf.size(); int extracted = 0; pm = new ProgressMonitor(getParent(), "Extracting files...", "starting", 0, size-4); pm.setMillisToDecideToPopup(0); pm.setMillisToPopup(0); Enumeration entries = zf.entries(); for (int i=0; i<size; i++) { ZipEntry entry = (ZipEntry) entries.nextElement(); if(entry.isDirectory()) continue; String pathname = entry.getName(); if(myClassName.equals(pathname) || MANIFEST.equals(pathname.toUpperCase())) continue; extracted ++; pm.setProgress(i); pm.setNote(pathname); if(pm.isCanceled()) return; in = zf.getInputStream(entry); File outFile = new File(outputDir, pathname); Date archiveTime = new Date(entry.getTime()); if(overwrite==false) { if(outFile.exists()) { Object[] options = {"Yes", "Yes To All", "No"}; Date existTime = new Date(outFile.lastModified()); Long archiveLen = new Long(entry.getSize()); String msg = "File name conflict: " + "There is already a file with " + "that name on the disk!\n" + "\nFile name: " + outFile.getName() + "\nExisting file: " + formatter.format(existTime) + ", " + outFile.length() + "Bytes" + "\nFile in archive:" + formatter.format(archiveTime) + ", " + archiveLen + "Bytes" +"\n\nWould you like to overwrite the file?"; int result = JOptionPane.showOptionDialog(ZipSelfExtractor.this, msg, "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options,options[0]); if(result == 2) // No { continue; } else if( result == 1) //YesToAll { overwrite = true; } } } File parent = new File(outFile.getParent()); if (parent != null && !parent.exists()) { parent.mkdirs(); } out = new FileOutputStream(outFile); while (true) { int nRead = in.read(buf, 0, buf.length); if (nRead <= 0) break; out.write(buf, 0, nRead); } out.close(); outFile.setLastModified(archiveTime.getTime()); } pm.close(); zf.close(); getToolkit().beep(); JOptionPane.showMessageDialog (ZipSelfExtractor.this, "Extracted " + extracted + " file" + ((extracted > 1) ? "s": "") + " from the\n" + zipfile + "\narchive into the\n" + outputDir.getPath() + "\ndirectory.", "Zip Self Extractor", JOptionPane.INFORMATION_MESSAGE); } catch (Exception e) { System.out.println(e); if(zf!=null) { try { zf.close(); } catch(IOException ioe) {;} } if(out!=null) { try {out.close();} catch(IOException ioe) {;} } if(in!=null) { try { in.close(); } catch(IOException ioe) {;} } } } } set JAVA_HOME=C:\jdk1.4.1%JAVA_HOME%\bin\java -classpath ".\%JAVA_HOME%\lib\tools.jar" 包名.类名(带有MAIN方法的类名字) 把上面这些存成.bat文件,运行即可。把你的.jar包放在classpath后面的""里面,中间用;隔开,还有JAVA_HOME也的改 把所有文件打包中jar文件就可以 //:jar用法之将文件打入jar文件中并使用.txta、文件路径如下:f:\java\testjar.javaf:\java\MANIFEST.MFf:\java\citi\sonnet.xml1、源文件//:testjar.javapackage citi;import java.io.*;public class testjar{ public static void main(String[] args){ try{ BufferedReader in; String str; //由于文件sonnet.xml要打入包中,故文件应该在包路径“citi”中 //使用时,需要使用相对路径来引用,如下面的".\\citi\\sonnet.xml" in = new BufferedReader(new FileReader(".\\citi\\sonnet.xml")); while((str = in.readLine())!= null){ System.out.println(str); } in.close(); }catch(Exception e){e.printStackTrace();} }}2、文件sonnet.xml内容<?xml version="1.0"?><sonnet type="Shakespearean"> <author>tsc</author></sonnet>3、文件MANIFEST.MF内容Manifest-Version: 1.0Main-Class: citi.testjarCreated-By: tangshancheng4、操作步骤实例F:\java>javac testjar.java -d .F:\java>jar cvfm my.jar manifest.mf citi标明清单(manifest)增加:citi/(读入= 0) (写出= 0)(存储了 0%)增加:citi/testjar.class(读入= 707) (写出= 464)(压缩了 34%)增加:citi/sonnet.dtd(读入= 1165) (写出= 361)(压缩了 69%)增加:citi/sonnet.xml(读入= 91) (写出= 80)(压缩了 12%)F:\java>java -jar my.jar<?xml version="1.0"?><sonnet type="Shakespearean"> <author>tsc</author></sonnet>不用包的话,可以作相应的修改注释package语句修改Main-Class: citi.testjar成Main-Class: testjar 请问各位大侠 在J2SE swing中能添加图片的组件有哪些啊!!! 知道多少说多少 最好还有对应的方法哦 越多越好 关于接口的概念问题 java 与sqlserver 2000 连接问题?! 毫秒数转换成时分秒格式 jtable更新问题,可以更新jtable里的数据,但是界面显示部正常 第一次在JAVA版发问,希望大家来帮助啊,一个关于类指针的问题! 关于Java I/O? java不能开发出好的界面来,怀念VC++ Applet能否访问服务器端的数据库? 各位大侠推荐几本学习JBUILDER的好书吧? JBuilder中的光标问题,解决就有分 Java程序接力,希望大家响应
用jar命令就正常了。偶觉得很奇怪。
我们先来了解一下自解压文件,在window下可以用自解压制作工具如winzip self-Extractor来制作自解压文件,这些工具会把一个zip文件与解压程序打包在一起而产生一个新的可执行文件。然后只要运行这个可执行文件,就可以把zip文件中的内容解开。那为什么要创建自解压jar文件呢,创建成自解压zip文件不就好了?我们应该注意到自解压jar文件可以在任意支持java的平台上解开并执行,例如,可以在linux下执行。创建jar自解压文件很简单,只需要一个特殊的JAR manifest文件、一个基于java的解压程序(这个程序是原作者写的)、包含基本文件的jar 或者zip文件以及任何jsdk的jar应用程序 manifest文件
要生成可执行jar文件,需要在META-INF 目录下的manifest文件,文件名为:MANIFEST.MF ,但在我们这里我们只需要在文件中指定在这个基于java 的解压程序中包含main()的类的名称:Main-Class: ZipSelfExtractor 我们已经把一个叫做jarmanifest的文件加入到这个技巧的源程序包中。 解压程序
你可以用各种方法来实现这个解压程序,在我们这里使用了一个简单直接的办法。首先,解压程序判断这个自解压jar文件的名称,有了这个文件名,解压程序使用解压标准,把文件解开。具体的可以查看在源码包中的ZipSelfExtractor.java文件。 值得一提的是这里用了一个很巧妙的办法获取jar文件的文件名,虽然在命令行中出现的这个文件的名字,但它并没有作为参数传入类的main()中,因此,这里使用了以下的代码来获取文件名: private String getJarFileName ()
{
myClassName = this.getClass().getName() + ".class";
URL urlJar =
this.getClass().getClassLoader().getSystemResource(myClassName);
String urlStr = urlJar.toString();
int from = "jar:file:".length();
int to = urlStr.indexOf("!/");
return urlStr.substring(from, to);
} 请注意:getSystemResource() 中使用了myClassName而不是ZipSelfExtractor.class作参数,这使得我们可以更改加压程序的名字而不需要修改代码。 接下来,我们来分析获得这个jar文件的名字。首先,可以获取指向包含正在运行类的文件,urlStr = urlJar.toString();有了这个url,把jar文件名去掉,剩下的就是我们想要的,下面是这个url的格式:
jar:file:/home/test/zipper.jar!/ZipSelfExtractor.class 有了文件名,就可以开始解压,详细的解压算法请大家自己看源码。 为了可以更方便实用,程序使用了图形界面,程序中使用了JFileChooser类可以选择要解压的目标目录。 最后程序还确保不把这两个文件:manifest文件和extractor's .class(在我们这里是ZipSelfExtractor.class)文件也解出来,程序是用来解开原始的jar的内容,而这两个文件并属于jar原始内容。 打包jar文件
有了manifest文件与解压程序,我们就可以创建自解压jar文件了,以下是一个例子:
1.创建一个zip文件Myzip.zip
2.下载zipper.jar
3.把文件解到当前目录,准备制作自解压jar文件
java -jar zipper.jar
4.把zipper.class拷贝成 ZipSelfExtractor.class
5.把 myzip.zip 重命名为 myzip.jar
6.把myzip.jar中的内容替换为jarmanifest和ZipSelfExtractor.class这两个文件
jar uvfm myzip.jar jarmanifest ZipSelfExtractor.class
7.执行java -jar myzip.jar就可以看到效果了,试试看 后记
一个自解压的jar文件能够很好的跨平台使用,自解压jar文件创建简单,只需要有jre1.2或或者更新的版本就可以实现了。 附自解压程序的源代码: /* ZipSelfExtractor.java */
/* Author: Z.S. Jin
Updates: John D. Mitchell */ import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.zip.*;
import java.util.*;
import java.text.*; public class ZipSelfExtractor extends JFrame
{
private String myClassName;
static String MANIFEST = "META-INF/MANIFEST.MF"; public static void main(String[] args)
{
ZipSelfExtractor zse = new ZipSelfExtractor();
String jarFileName = zse.getJarFileName();
zse.extract(jarFileName);
System.exit(0);
} ZipSelfExtractor()
{
} private String getJarFileName()
{
myClassName = this.getClass().getName() + ".class";
URL urlJar = this.getClass().getClassLoader().getSystemResource(myClassName);
String urlStr = urlJar.toString();
int from = "jar:file:".length();
int to = urlStr.indexOf("!/");
return urlStr.substring(from, to);
} public void extract(String zipfile)
{
File currentArchive = new File(zipfile); JFileChooser fc = new JFileChooser(); fc.setCurrentDirectory(new File("."));
fc.setDialogType(JFileChooser.OPEN_DIALOG);
fc.setDialogTitle("Select destination directory for extracting " +
currentArchive.getName());
fc.setMultiSelectionEnabled(false); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if (fc.showDialog(ZipSelfExtractor.this, "Select")
!= JFileChooser.APPROVE_OPTION)
{
return; //only when user select valid dir, it can return approve_option
} File outputDir = fc.getSelectedFile(); byte[] buf = new byte[1024];
SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yyyy hh:mma",Locale.getDefault()); ProgressMonitor pm = null; boolean overwrite = false; ZipFile zf = null;
FileOutputStream out = null;
InputStream in = null; try
{
zf = new ZipFile(currentArchive); int size = zf.size();
int extracted = 0;
pm = new ProgressMonitor(getParent(), "Extracting files...", "starting", 0, size-4);
pm.setMillisToDecideToPopup(0);
pm.setMillisToPopup(0); Enumeration entries = zf.entries(); for (int i=0; i<size; i++)
{
ZipEntry entry = (ZipEntry) entries.nextElement();
if(entry.isDirectory())
continue; String pathname = entry.getName();
if(myClassName.equals(pathname) || MANIFEST.equals(pathname.toUpperCase()))
continue; extracted ++;
pm.setProgress(i);
pm.setNote(pathname);
if(pm.isCanceled())
return; in = zf.getInputStream(entry); File outFile = new File(outputDir, pathname);
Date archiveTime = new Date(entry.getTime()); if(overwrite==false)
{
if(outFile.exists())
{
Object[] options = {"Yes", "Yes To All", "No"};
Date existTime = new Date(outFile.lastModified());
Long archiveLen = new Long(entry.getSize()); String msg = "File name conflict: "
+ "There is already a file with "
+ "that name on the disk!\n"
+ "\nFile name: " + outFile.getName()
+ "\nExisting file: "
+ formatter.format(existTime) + ", "
+ outFile.length() + "Bytes"
+ "\nFile in archive:"
+ formatter.format(archiveTime) + ", "
+ archiveLen + "Bytes"
+"\n\nWould you like to overwrite the file?"; int result = JOptionPane.showOptionDialog(ZipSelfExtractor.this,
msg, "Warning", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options,options[0]); if(result == 2) // No
{
continue;
}
else if( result == 1) //YesToAll
{
overwrite = true;
}
}
} File parent = new File(outFile.getParent());
if (parent != null && !parent.exists())
{
parent.mkdirs();
} out = new FileOutputStream(outFile); while (true)
{
int nRead = in.read(buf, 0, buf.length);
if (nRead <= 0)
break;
out.write(buf, 0, nRead);
} out.close();
outFile.setLastModified(archiveTime.getTime());
} pm.close();
zf.close();
getToolkit().beep(); JOptionPane.showMessageDialog
(ZipSelfExtractor.this,
"Extracted " + extracted +
" file" + ((extracted > 1) ? "s": "") +
" from the\n" +
zipfile + "\narchive into the\n" +
outputDir.getPath() +
"\ndirectory.",
"Zip Self Extractor",
JOptionPane.INFORMATION_MESSAGE); }
catch (Exception e)
{
System.out.println(e);
if(zf!=null) { try { zf.close(); } catch(IOException ioe) {;} }
if(out!=null) { try {out.close();} catch(IOException ioe) {;} }
if(in!=null) { try { in.close(); } catch(IOException ioe) {;} }
}
}
}
%JAVA_HOME%\bin\java -classpath ".\%JAVA_HOME%\lib\tools.jar" 包名.类名(带有MAIN方法的类名字) 把上面这些存成.bat文件,运行即可。
把你的.jar包放在classpath后面的""里面,中间用;隔开,还有JAVA_HOME也的改
f:\java\testjar.java
f:\java\MANIFEST.MF
f:\java\citi\sonnet.xml1、源文件
//:testjar.java
package citi;
import java.io.*;
public class testjar{
public static void main(String[] args){
try{
BufferedReader in;
String str;
//由于文件sonnet.xml要打入包中,故文件应该在包路径“citi”中
//使用时,需要使用相对路径来引用,如下面的".\\citi\\sonnet.xml"
in = new BufferedReader(new FileReader(".\\citi\\sonnet.xml"));
while((str = in.readLine())!= null){
System.out.println(str);
}
in.close();
}catch(Exception e){e.printStackTrace();}
}
}2、文件sonnet.xml内容
<?xml version="1.0"?>
<sonnet type="Shakespearean">
<author>tsc</author>
</sonnet>3、文件MANIFEST.MF内容
Manifest-Version: 1.0
Main-Class: citi.testjar
Created-By: tangshancheng4、操作步骤实例F:\java>javac testjar.java -d .F:\java>jar cvfm my.jar manifest.mf citi
标明清单(manifest)
增加:citi/(读入= 0) (写出= 0)(存储了 0%)
增加:citi/testjar.class(读入= 707) (写出= 464)(压缩了 34%)
增加:citi/sonnet.dtd(读入= 1165) (写出= 361)(压缩了 69%)
增加:citi/sonnet.xml(读入= 91) (写出= 80)(压缩了 12%)F:\java>java -jar my.jar
<?xml version="1.0"?>
<sonnet type="Shakespearean">
<author>tsc</author>
</sonnet>
不用包的话,可以作相应的修改
注释package语句
修改Main-Class: citi.testjar成Main-Class: testjar