简体   繁体   English

如何为我的程序生成一个工作jar文件?

[英]How to generate a working jar file for my program?

I am rather new to java, in fact this is my first program. 我对java很新,实际上这是我的第一个程序。 I seem to be having a problem with generating a jar file for my java program. 我似乎在为我的java程序生成一个jar文件时遇到问题。 It is a very basic program that merely grabs data from a website, then displays it in a gui. 这是一个非常基本的程序,只是从网站上抓取数据,然后在gui中显示它。 The program is dependent on Jsoup and HtmlCleaner. 该程序依赖于Jsoup和HtmlCleaner。 The gui was made with JFormDesigner. gui是用JFormDesigner制作的。

Getting back to the problem, when I run the program using eclipse everything runs perfectly. 回到这个问题,当我使用eclipse运行程序时,一切都运行得很好。 No issues whatsoever. 没有任何问题。 However, when I run it as an exported jar (or a jar made with Jar Maker), the GUI doesn't even show up. 但是,当我将它作为导出的jar(或使用Jar Maker制作的jar)运行时,GUI甚至都不显示。 The strange thing is that when I run it with a .bat file, the GUI displays, but the program doesn't do what it is suppose to do. 奇怪的是,当我使用.bat文件运行它时,GUI会显示,但是程序没有按照预期的那样做。

I am really stuck, I am not sure what to try next. 我真的被卡住了,我不知道下一步该尝试什么。 Any help will be greatly appreciated. 任何帮助将不胜感激。

//package com.stackoverflow.q2835505;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.awt.*;

import javax.swing.*;

import com.sun.xml.internal.ws.util.StringUtils;

public class TestDumper extends JFrame {
public static String test = "+";
public static String test1 = "-";
public static String itemID;
public static String stab;
public static String slash;
public static String crush;
public static String magic;
public static String range;
public static String stabDef;
public static String slashDef;
public static String crushDef;
public static String magicDef;
public static String rangeDef;
public static String Str;
public static String Prayer;
public static String lowAlch;
public static String highAlch;
public static String Name;
public boolean guiWait = false;
public static boolean printed = true;



    public static void main(String[] args) throws Exception {
        statGrabber g = new statGrabber();
        g.setDefaultCloseOperation(EXIT_ON_CLOSE);
        g.setVisible(true);

        for ( ; ; ) {
        while(g.test){
        String url = g.textField2.getText();
        Document document = Jsoup.connect(url).get();
        stab = document.select("table.wikitable.smallpadding tbody tr td").tagName("TD").eq(2).text();
        slash = document.select("table.wikitable.smallpadding tbody tr td").tagName("TD").eq(3).text();
        crush = document.select("table.wikitable.smallpadding tbody tr td").tagName("TD").eq(4).text();
        magic = document.select("table.wikitable.smallpadding tbody tr td").tagName("TD").eq(5).text();
        range = document.select("table.wikitable.smallpadding tbody tr td").tagName("TD").eq(6).text();
        stabDef = document.select("table.wikitable.smallpadding tbody tr td").tagName("TD").eq(7).text();
        slashDef = document.select("table.wikitable.smallpadding tbody tr td").tagName("TD").eq(8).text();
        crushDef = document.select("table.wikitable.smallpadding tbody tr td").tagName("TD").eq(9).text();
        magicDef = document.select("table.wikitable.smallpadding tbody tr td").tagName("TD").eq(10).text();
        rangeDef = document.select("table.wikitable.smallpadding tbody tr td").tagName("TD").eq(11).text();
        Str = document.select("table.wikitable.smallpadding tbody tr td").tagName("TD").eq(16).text();
        Prayer = document.select("table.wikitable.smallpadding tbody tr td").tagName("TD").eq(17).text();
        Name = document.select("table.wikitable.infobox caption b").text();
        highAlch = document.select("table.wikitable.infobox tbody tr td").tagName("TD").eq(8).text();
        lowAlch =  document.select("table.wikitable.infobox tbody tr td").tagName("TD").eq(9).text();
        g.sweet = true;
       // String shopPrice = document.select("table.wikitable.infobox tbody tr td").tagName("TD").eq(11).text();


       if(stab.startsWith(test)){
           stab = stab.substring(1);
       }
       if(slash.startsWith(test)){
           slash = slash.substring(1);
       }
       if(crush.startsWith(test)){
           crush = crush.substring(1);
       }
       if(magic.startsWith(test)){
           magic.trim();
           magic = magic.substring(1);
       }
       if(range.startsWith(test)){
           range.trim();
           range = range.substring(1);
       }
       if(stabDef.startsWith(test)){
           stabDef = stabDef.substring(1);
       }
       if(slashDef.startsWith(test)){
           slashDef = slashDef.substring(1);
       }
       if(crushDef.startsWith(test)){
           crushDef = crushDef.substring(1);
       }
       if(magicDef.startsWith(test)){
           magicDef.trim();
           magicDef = magicDef.substring(1);
       }
       if(rangeDef.startsWith(test)){
           rangeDef.trim();
           rangeDef = rangeDef.substring(1);
       }
       if(Str.startsWith(test)){
           Str.trim();
           Str = Str.substring(1);
       }
       if(Prayer.startsWith(test)){
           Prayer.trim();
           Prayer = Prayer.substring(1);
       }


       if(highAlch.equalsIgnoreCase("Cannot be alchemised")){
           highAlch = "0";
           lowAlch = "0";
       }
       else{
           highAlch = highAlch.replace(",", "");
           highAlch = highAlch.replace(" ", "");
           highAlch = highAlch.replace("c", "");
           highAlch = highAlch.replace("o", "");
           highAlch = highAlch.replace("i", "");
           highAlch = highAlch.replace("n", "");
           highAlch = highAlch.replace("s", "");

           lowAlch = lowAlch.replace(",", "");
           lowAlch = lowAlch.replace(" ", "");
           lowAlch = lowAlch.replace("c", "");
           lowAlch = lowAlch.replace("o", "");
           lowAlch = lowAlch.replace("i", "");
           lowAlch = lowAlch.replace("n", "");
           lowAlch = lowAlch.replace("s", "");
       }

       while(printed){
        System.out.print("item = "+ itemID+"\t");
        System.out.print(Name.replace(" ", "_")+"\t");
        System.out.print(Name.replace(" ", "_")+"\t");
        System.out.print(highAlch+"\t");
        System.out.print(lowAlch+"\t");
        System.out.print("0\t");
        System.out.print(stab+"\t");
        System.out.print(slash+"\t");
        System.out.print(crush+"\t");
        System.out.print(magic+"\t");
        System.out.print(range+"\t");
        System.out.print(stabDef+"\t");
        System.out.print(slashDef+"\t");
        System.out.print(crushDef+"\t");
        System.out.print(magicDef+"\t");
        System.out.print(rangeDef+"\t");
        System.out.print(Str+"\t");
        System.out.print(Prayer+"\n");
        System.out.print("Connected!\n");
        printed = false;
       }
        }
    }
    }
    }  

In eclipse, you can right click on project name 在eclipse中,您可以右键单击项目名称

  • select export 选择导出
  • in Java categories select Runable JAR file 在Java类别中选择Runable JAR文件
  • select the configuration of main class (if it not found, you should run your app before) 选择主类的配置(如果没有找到,你应该先运行你的应用程序)
  • set export destination 设置导出目的地
  • select copy require libs in to sub-folder select copy要求libs到子文件夹
  • finish

When you export a jar, you can specify dependency jars as Class-Path entry in jar manifest.mf file. 导出jar时,可以在jar manifest.mf文件中将依赖关系jar指定为Class-Path条目。 You can find more details about this in this link: http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html . 您可以在以下链接中找到有关此内容的更多详细信息: http//docs.oracle.com/javase/tutorial/deployment/jar/downman.html Once you have dont this make sure respective dependent jars will be in classpath. 一旦你没有这个确保相应的从属罐将在类路径中。

You can use Eclipse IDE to generate .jar files. 您可以使用Eclipse IDE生成.jar文件。 Also you are able to add your resource files such as images or media files into .jar . 您还可以将资源文件(如图像或媒体文件)添加到.jar中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM