当前位置: 技术问答>java相关
搞了3小时都不出来。高分相送(jar文件出错)急~~~~~~~
来源: 互联网 发布时间:2015-10-21
本文导语: import java.awt.*; import java.awt.event.*; public class MyClass { public static void main(String[] args) { Frame f = new Frame(); f.addWindowListener (new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } );...
import java.awt.*;
import java.awt.event.*;
public class MyClass
{
public static void main(String[] args)
{
Frame f = new Frame();
f.addWindowListener
(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
f.add(new Label("Hello world"));
f.setSize(200,200);
f.setVisible(true);
}
}
然后创建这个manifest 文件(manifest.mft) (在bin下)
Manifest-Version: 1.0
Main-Class: MyClass
Classpath: .MyJar.jar
保存为mainfest.mft
在 dos 窗口下运行
jar cvfm MyJar.jar manifest.mft MyClass.class
出错:
Exception in thread "main" java.util.zip.ZipException: 系统找不到指定的文件。
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.(ZipFile.java:112)
at java.util.jar.JarFile.(JarFile.java:117)
at java.util.jar.JarFile.(JarFile.java:55)
请大家指点呀!!!!!!!!!!!!
小弟感激不尽!
import java.awt.event.*;
public class MyClass
{
public static void main(String[] args)
{
Frame f = new Frame();
f.addWindowListener
(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
f.add(new Label("Hello world"));
f.setSize(200,200);
f.setVisible(true);
}
}
然后创建这个manifest 文件(manifest.mft) (在bin下)
Manifest-Version: 1.0
Main-Class: MyClass
Classpath: .MyJar.jar
保存为mainfest.mft
在 dos 窗口下运行
jar cvfm MyJar.jar manifest.mft MyClass.class
出错:
Exception in thread "main" java.util.zip.ZipException: 系统找不到指定的文件。
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.(ZipFile.java:112)
at java.util.jar.JarFile.(JarFile.java:117)
at java.util.jar.JarFile.(JarFile.java:55)
请大家指点呀!!!!!!!!!!!!
小弟感激不尽!
|
Manifest你不要自己写Classpath.用自己生成的
jar cvf MyJar.jar MyClass.class .
jar cvf MyJar.jar MyClass.class .
|
jar cvf MyJar.jar MyClass.class
以下这个程序可以实现将jar文件和含有main的类关联的功能,不过是别人
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.*;
/**
* This utility makes a JAR runnable by inserting a Main-Class
* attribute into the Manifest.
*
* @author Shawn Silverman
*/
public class MakeJarRunnable {
public static void main(String[] args) throws IOException {
if (args.length != 3) {
System.out.println("Usage: MakeJarRunnable "
+ " ");
System.exit(0);
}
// Create the JarInputStream object, and get its Manifest
JarInputStream jarIn = new JarInputStream(new FileInputStream(args[0]));
Manifest manifest = jarIn.getManifest();
if (manifest == null) {
// This will happen if there is no Manifest
manifest = new Manifest();
}
Attributes a = manifest.getMainAttributes();
String oldMainClass = a.putValue("Main-Class", args[1]);
// If there was an old value there, tell the user about it and exit
if (oldMainClass != null) {
System.out.println("Warning: old Main-Class value is: "
+ oldMainClass);
System.exit(1);
}
System.out.println("Writing to " + args[2] + "...");
JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(args[2]),
manifest);
// Create a read buffer to be used for transferring data from the input
byte[] buf = new byte[4096];
// Iterate the entries
JarEntry entry;
while ((entry = jarIn.getNextJarEntry()) != null) {
// Exclude the Manifest file from the old JAR
if ("META-INF/MANIFEST.MF".equals(entry.getName())) continue;
// Write out the entry to the output JAR
jarOut.putNextEntry(entry);
int read;
while ((read = jarIn.read(buf)) != -1) {
jarOut.write(buf, 0, read);
}
jarOut.closeEntry();
}
// Flush and close all the streams
jarOut.flush();
jarOut.close();
jarIn.close();
}
}
制作可运行的jar文件:
java MakeJarRunnable myjar.jar HelloRunnableWorld myjar_r.jar
myjar.jar表示要制作成可运行的已做成jar的源文件。HelloRunnableWorld 表示包含main的类(不包括.class后缀)。myjar_r.jar表示可运行的jar文件。
在myjar_r.jar中的META-INF目录的MANIFEST.MF文件中要比普通的jar文件多一行:Main-Class: HelloRunnableWorld
运行:java -jar myjar_r.jar
以下这个程序可以实现将jar文件和含有main的类关联的功能,不过是别人
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.*;
/**
* This utility makes a JAR runnable by inserting a Main-Class
* attribute into the Manifest.
*
* @author Shawn Silverman
*/
public class MakeJarRunnable {
public static void main(String[] args) throws IOException {
if (args.length != 3) {
System.out.println("Usage: MakeJarRunnable "
+ " ");
System.exit(0);
}
// Create the JarInputStream object, and get its Manifest
JarInputStream jarIn = new JarInputStream(new FileInputStream(args[0]));
Manifest manifest = jarIn.getManifest();
if (manifest == null) {
// This will happen if there is no Manifest
manifest = new Manifest();
}
Attributes a = manifest.getMainAttributes();
String oldMainClass = a.putValue("Main-Class", args[1]);
// If there was an old value there, tell the user about it and exit
if (oldMainClass != null) {
System.out.println("Warning: old Main-Class value is: "
+ oldMainClass);
System.exit(1);
}
System.out.println("Writing to " + args[2] + "...");
JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(args[2]),
manifest);
// Create a read buffer to be used for transferring data from the input
byte[] buf = new byte[4096];
// Iterate the entries
JarEntry entry;
while ((entry = jarIn.getNextJarEntry()) != null) {
// Exclude the Manifest file from the old JAR
if ("META-INF/MANIFEST.MF".equals(entry.getName())) continue;
// Write out the entry to the output JAR
jarOut.putNextEntry(entry);
int read;
while ((read = jarIn.read(buf)) != -1) {
jarOut.write(buf, 0, read);
}
jarOut.closeEntry();
}
// Flush and close all the streams
jarOut.flush();
jarOut.close();
jarIn.close();
}
}
制作可运行的jar文件:
java MakeJarRunnable myjar.jar HelloRunnableWorld myjar_r.jar
myjar.jar表示要制作成可运行的已做成jar的源文件。HelloRunnableWorld 表示包含main的类(不包括.class后缀)。myjar_r.jar表示可运行的jar文件。
在myjar_r.jar中的META-INF目录的MANIFEST.MF文件中要比普通的jar文件多一行:Main-Class: HelloRunnableWorld
运行:java -jar myjar_r.jar
|
反正你的文件不多,就用楼上的方法就行了
|
看看这个帖子,jar命令的解释