黑马程序员技术交流社区
标题:
你Win7中的Jar包能执行吗?
[打印本页]
作者:
小黑子
时间:
2014-9-27 14:42
标题:
你Win7中的Jar包能执行吗?
本帖最后由 小黑子 于 2014-9-29 17:49 编辑
跟着毕老师的视频,写了一个简易的记事本小程序,只有:打开、保存、退出 功能。
本来程序通过DOS是可以正常启动运行的。可是,为了打包成Jar,在首行加了 package mymenu; 并将类public
这时再通过DOS,编译正常,可是运行会提示:Error: Could not find or load main class MyFileDialogDemo
我也不知道这正不正常。。。。然后就继续往下做了。
1、通过: javac -d c:\myclass MyFileDialogDemo.java 将编译生成的几个类放到了 C:\myclass\mymenu 下
2、在C:\myclass下建立1.txt,并输入如下内容:
Main-Class: mymenu.MyFileDialogDemo
该注意的空格、回车我都有了。用好压打开my.jar中的MAINFEST.MF可以看到,上边这行信息也确实写进去了。
3、通过 jar -cvfm my.jar 1.txt mymenu 生成my.jar
4、因为我是Win7系统,我在my.jar上 右击--》打开方式 选择 javaw.exe为jar文件的默认打开方式。并将百度上教的那一套,在注册表中也加了-jar
可是,我双击jar就是不运行啊。不过在刚刚双击完那一会,鼠标的小箭头旁边会有一个小圆圈转一下。。。应该还是有什么东西在试图运行的。
package mymenu;
import java.awt.Frame;
import java.awt.MenuBar;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.FileDialog;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.File;
public class MyFileDialogDemo
{
private Frame f;
private MenuBar bar;
private Menu file;
private MenuItem openItem,saveItem,exitItem;
private TextArea ta;
private FileDialog fdLoad,fdSave;
private File fl;
MyFileDialogDemo()
{
initialize();
}
public void initialize()
{
f = new Frame("WhatEver");
f.setBounds(300,200,600,500);
f.setVisible(true);
bar = new MenuBar();
file = new Menu("文件");
openItem = new MenuItem("打开");
saveItem = new MenuItem("保存");
exitItem = new MenuItem("退出");
ta = new TextArea();
fdLoad = new FileDialog(f,"打开",FileDialog.LOAD);
fdSave = new FileDialog(f,"保存",FileDialog.SAVE);
f.setMenuBar(bar);
f.add(ta);
bar.add(file);
file.add(openItem);
file.add(saveItem);
file.add(exitItem);
myEvent();
}
public void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
saveItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(fl==null)
{
fdSave.setVisible(true);
String path = fdSave.getDirectory();
String name = fdSave.getFile();
if(path==null || name==null)
return;
fl = new File(path+name);
}
try
{
BufferedWriter bw = new BufferedWriter(new FileWriter(fl));
String text = ta.getText();
bw.write(text);
bw.close();
}
catch (IOException ex)
{
throw new RuntimeException("保存失败");
}
}
});
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fdLoad.setVisible(true);
String path = fdLoad.getDirectory();
String name = fdLoad.getFile();
if(path==null || name==null)
return;
ta.setText("");
try
{
BufferedReader br = new BufferedReader(new FileReader(path+name));
String line;
while((line=br.readLine())!=null)
{
ta.append(line+"\r\n");
}
br.close();
}
catch (IOException ex)
{
throw new RuntimeException("打开失败");
}
}
});
}
public static void main(String[] args)
{
new MyFileDialogDemo();
}
}
复制代码
jar1.png
(40.05 KB, 下载次数: 29)
下载附件
2014-9-27 14:42 上传
jar2.png
(139.52 KB, 下载次数: 15)
下载附件
2014-9-27 14:42 上传
作者:
小黑子
时间:
2014-9-27 15:13
有大神知道不?
作者:
小黑子
时间:
2014-9-28 11:47
No body knows?:Q
作者:
小黑子
时间:
2014-9-28 17:38
求解啊:'(你们的都能执行吗?
作者:
小黑子
时间:
2014-9-29 07:44
有木有相同情况的兄弟啊
作者:
小黑子
时间:
2014-9-29 17:50
悬而未决……………………
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2