Java JAR不是';下载时不兼容
我为GUI更新系统创建了一个JAR,该系统在其内部下载一个可执行文件并执行它。无论何时运行原始副本,这都非常适合我。但是,从Internet下载时,GUI会中断。页眉字体恢复为原始字体,“下载”按钮将激活“计划”对话框,但其中与下载文件无关。请记住,我的Java JAR不是';下载时不兼容,java,windows,jar,download,Java,Windows,Jar,Download,我为GUI更新系统创建了一个JAR,该系统在其内部下载一个可执行文件并执行它。无论何时运行原始副本,这都非常适合我。但是,从Internet下载时,GUI会中断。页眉字体恢复为原始字体,“下载”按钮将激活“计划”对话框,但其中与下载文件无关。请记住,我的setup.jar的原始版本可以工作,但下载的版本没有任何功能 可以找到JAR,但是对于直接下载,您可以找到它 下面是我的JAVA文件的内容&JAR层次结构的图片: public class UpdateMechanism extends JFr
setup.jar的原始版本可以工作,但下载的版本没有任何功能
可以找到JAR,但是对于直接下载,您可以找到它
下面是我的JAVA文件的内容&JAR层次结构的图片:
public class UpdateMechanism extends JFrame
implements ActionListener {
protected static JButton aroundTheLake;
protected static JFrame frame;
private static JButton aroundTheRiver() {
aroundTheLake = new JButton("DOWNLOAD & INSTALL!");
aroundTheLake.setVerticalTextPosition(AbstractButton.CENTER);
aroundTheLake.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
aroundTheLake.addActionListener(new UpdateMechanism());
aroundTheLake.setMnemonic(KeyEvent.VK_D);
aroundTheLake.setActionCommand("aroundthelake");
return aroundTheLake;
}
private static String readURL(String targetURL) {
String returnish = "";
try {
URL tempURL = new URL(targetURL);
Scanner s = new Scanner(tempURL.openStream());
while (s.hasNextLine()) {
returnish = returnish+s.nextLine();
}
} catch (IOException e) {
System.out.println(e);
}
return returnish;
}
private static String readFile(String targetFile) {
String returnString = "";
try {
File tempFile = new File(targetFile);
Scanner s = new Scanner(tempFile);
while (s.hasNextLine()) {
returnString = returnString + s.nextLine();
}
} catch(IOException e) {
// !
System.out.println(e);
}
return returnString;
}
public void actionPerformed(ActionEvent e) {
if ("aroundthelake".equals(e.getActionCommand())) {
JOptionPane.showMessageDialog(frame,
"Please wait, this may take a while depending on your connection...");
new Thread() {
public void run() {
URL url;
URLConnection con;
DataInputStream dis;
FileOutputStream fos;
byte[] fileData;
try {
url = new URL("http://hivelocity.dl.sourceforge.net/project/theneverhood/setup.exe");
con = url.openConnection();
dis = new DataInputStream(con.getInputStream());
fileData = new byte[con.getContentLength()];
for (int x = 0; x < fileData.length; x++) {
fileData[x] = dis.readByte();
}
dis.close();
fos = new FileOutputStream(new File("executable/setup.exe"));
fos.write(fileData);
fos.close();
Runtime.getRuntime().exec("executable/setup.exe", null, new File("executable/"));
}
catch(MalformedURLException m) {
System.out.println(m);
}
catch(IOException io) {
System.out.println(io);
}
}
}.start();
} else {
// man
}
}
private static void showGUI() {
frame = new JFrame("The Neverhood Restoration Project");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(1024, 600));
frame.setExtendedState(frame.MAXIMIZED_BOTH);
frame.getContentPane().setBackground(new Color(0xA64343));
File fileCheck = new File("C:/Program Files (x86)");
String returnString = null;
String rootDirectory = null;
if (fileCheck.exists()) {
rootDirectory = "C:/Program Files (x86)/DreamWorks Interactive";
String checkFile = rootDirectory+"/Neverhood/version.txt";
File tempFile = new File(checkFile);
if (tempFile.exists()) {
returnString = readFile(checkFile);
} else {
returnString = "It appears you do not have the Neverhood Restoration Project installed, or you are using an earlier version.";
}
} else {
rootDirectory = "C:/Program Files/DreamWorks Interactive";
String checkFile = rootDirectory+"/Neverhood/version.txt";
File tempFile = new File(checkFile);
if (tempFile.exists()) {
returnString = readFile(checkFile);
} else {
returnString = "It appears you do not have the Neverhood Restoration Project installed, or you are using an earlier version.";
}
}
if (returnString.equals(readURL("http://theneverhood.sourceforge.net/version.txt"))) {
returnString = "You are updated to the recent version!";
} else {
returnString = "It appears you're not updated.";
}
JLabel headerLabel = new JLabel("The Neverhood Restoration Project");
headerLabel.setHorizontalAlignment(JLabel.CENTER);
JPanel heapPanel = new JPanel();
heapPanel.setLayout(new BoxLayout(heapPanel, BoxLayout.PAGE_AXIS));
heapPanel.setPreferredSize(new Dimension(500, heapPanel.getPreferredSize().height));
JTextArea heapLabel = new JTextArea(50, 50);
heapLabel.setLineWrap(true);
heapLabel.setWrapStyleWord(true);
heapLabel.setEditable(false);
heapLabel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
heapLabel.setFont(new Font("Serif", Font.PLAIN, 14));
heapLabel.append("Current version: "+readURL("http://theneverhood.sourceforge.net/prettyversion.txt")+".\nInstalled version: "+readFile(rootDirectory+"/Neverhood/prettyversion.txt")+".\n"+returnString+"\n" +
"You can read the full version of the document to the left at http://theneverhood.sourceforge.net."
+ "\nHaven't installed yet? Below is the download button. Just click to save setup.exe in and enjoy!");
heapPanel.add(heapLabel);
if (returnString == "It appears you're not updated.") {
heapPanel.add(aroundTheRiver());
}
try {
Font sFont = Font.createFont(Font.TRUETYPE_FONT, new File("DUGFB___.TTF"));
sFont = sFont.deriveFont(Font.PLAIN, 48);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(sFont);
headerLabel.setFont(sFont);
} catch (FontFormatException | IOException e) {
System.out.println(e);
}
BufferedImage icoImage = null;
try {
icoImage = ImageIO.read(
frame.getClass().getResource("/nhood.bmp"));
} catch (IOException e) {
System.out.println(e);
}
frame.setIconImage(icoImage);
JEditorPane updateLog = new JEditorPane();
JScrollPane scrollPane = new JScrollPane(updateLog);
updateLog.setEditable(false);
try {
updateLog.setPage("http://theneverhood.sourceforge.net/");
} catch (IOException e) {
updateLog.setContentType("text/html");
updateLog.setText("<html>The application could not load the webpage.</html>");
}
frame.add(headerLabel, BorderLayout.NORTH);
frame.add(scrollPane);
frame.add(heapPanel, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
showGUI();
}
});
}
}
公共类更新机制扩展JFrame
实现ActionListener{
湖泊周围受保护的静态按钮;
受保护的静态框架;
围绕服务器()的私有静态JButton{
aroundTheLake=newjbutton(“下载并安装!”);
在Lake.setVerticalTextPosition(AbstractButton.CENTER)周围;
aroundTheLake.setHorizontalTextPosition(AbstractButton.LEADING);//又名LEFT,用于从左到右的区域设置
围绕lake.addActionListener(新的UpdateMechanism());
围绕lake.set助记符(KeyEvent.VK_D);
setActionCommand(“aroundTheLake”);
绕湖返回;
}
私有静态字符串readURL(字符串targetURL){
字符串returnish=“”;
试一试{
URL tempURL=新URL(targetURL);
Scanner s=新的扫描仪(tempURL.openStream());
而(s.hasNextLine()){
returnish=returnish+s.nextLine();
}
}捕获(IOE异常){
系统输出打印ln(e);
}
返乡;
}
私有静态字符串读取文件(字符串目标文件){
字符串returnString=“”;
试一试{
File tempFile=新文件(targetFile);
扫描仪s=新扫描仪(临时文件);
而(s.hasNextLine()){
returnString=returnString+s.nextLine();
}
}捕获(IOE){
// !
系统输出打印ln(e);
}
返回字符串;
}
已执行的公共无效操作(操作事件e){
if(“aroundthelake.”等于(例如getActionCommand()){
JOptionPane.showMessageDialog(框架,
“请稍候,这可能需要一些时间,具体取决于您的连接…”);
新线程(){
公开募捐{
网址;
URLConnection;
数据输入流dis;
文件输出流;
字节[]文件数据;
试一试{
url=新url(“http://hivelocity.dl.sourceforge.net/project/theneverhood/setup.exe");
con=url.openConnection();
dis=新的DataInputStream(con.getInputStream());
fileData=新字节[con.getContentLength()];
对于(intx=0;x new Thread() {
public void run() {
URL url;
URLConnection con;
DataInputStream dis;
FileOutputStream fos;
byte[] fileData;
try {
url = new URL("http://hivelocity.dl.sourceforge.net/project/theneverhood/setup.exe");
con = url.openConnection();
dis = new DataInputStream(con.getInputStream());
fileData = new byte[con.getContentLength()];
for (int x = 0; x < fileData.length; x++) {
fileData[x] = dis.readByte();
}
dis.close();
File f = new File("executable");
if(!f.isDirectory())
f.mkdir();
fos = new FileOutputStream(new File("executable/setup.exe"));
fos.write(fileData);
fos.close();
Runtime.getRuntime().exec("executable/setup.exe", null, new File("executable/"));
}
catch(MalformedURLException m) {
System.out.println(m);
}
catch(IOException io) {
System.out.println(io);
}
}
}.start();
List<String> command = new ArrayList<String>();
command.add("cmd");
command.add("/C");
File dcr = new File("executable/setup.exe");
dcr.setExecutable(true, false);
command.add(dcr.getAbsolutePath());
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
final Process process = builder.start();
try {
process.waitFor();
} catch (InterruptedException ex) {
Logger.getLogger(WhatEver.class.getName()).log(Level.SEVERE, null, ex);
throw ex;
}
InputStream is = process.getErrorStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
Logger.getLogger(Thumbnail.class.getName()).severe(line);
}