创建可执行jar文件时出现问题

时间:2017-05-24 21:54:42

标签: java

我在Java中创建了一个漂亮的音乐生成器。代码不是很有效但它确实运行。我顺便使用BlueJ。 BlueJ有一个功能是创建一个可执行的jar文件,你可以选择使用哪个类的主要方法,然后编译它。我继续在我的计算机上获取损坏的jar文件,当我将程序交给朋友让他也这样做时,但是jar文件在点击时什么都不做。

程序引用项目文件夹中的音乐剪辑文件(.wav),但反编译时的jar文件没有它们。

它在控制台中运行,我刚刚得知控制台不能与Jar文件一起使用。很公平。如果我将此更改为JOptionPane用于用户输入,它会显示吗?我如何包含声音文件呢?

我看到有关使用控制台的一些内容,但我无法访问学校计算机上的内容。或者至少我不认为我这样做。我的最终目标是能够运行一个jar文件并让它播放。这样,我可以在我的主计算机chromebook上运行我的jar文件。

编辑:如果你想查看我的代码以查看我是否有错误,那么项目就在这里。 https://drive.google.com/file/d/0B2a2yK5zF36HWHc0X1JzMU1ZRDA/view?usp=sharing

2 个答案:

答案 0 :(得分:1)

你要问的是什么(你实际上得到了什么错误?为什么不包括* .wavs?)并不是很清楚,所以你冒着关闭问题的风险。 不过,我会尽力帮助你。

  • Jar文件只是一个简单的zip存档,包含已编译的类,资源(如* .wav文件)和一个特殊的MANIFEST.MF文件
  • jar中包含的任何资源都可以在代码中使用,有很多关于该主题的教程,但简而言之,您需要做的只是:InputStream wav = this.getClass().getResourceAsStream("sample.wav");
  • 是的,您可以从命令行运行jar:java -jar file.jar
  • 您需要通过上述命令制作一个jar可执行文件,或者只需双击一下就可以显示清单中的Main-Class条目,类似于:Main-Class: com.example.ClassWithMain
  • 是的,创建一个GUI应用程序会起作用,你的主要方法将负责创建windows等,但它是一个GUI应用程序和jar可执行文件之间没有任何关系。执行jar只意味着从清单中指定的类运行main方法。
  • 运行jar的机器需要(至少)安装JRE。如上所述,jar只是类的zip存档,而不是本机可执行文件。

答案 1 :(得分:0)

我认为您的问题可分为2:您可能没有在项目中包含* .wav文件,请确保在创建jar文件时选择AudioFiles文件夹:

enter image description here

此文件的路径也必须与jar文件的位置相关,因为应用程序不知道从哪个位置运行,我修改了主类代码来纠正这个问题。它在我的计算机上运行正常,从windows命令窗口输入“java -jar juke.jar”或者你放到jar文件中的任何名称。

修改后的代码:

    /**
 * Made by Aaron Cohen
 */

//http://introcs.cs.princeton.edu/java/assignments/mozart.html
import java.util.Random;
import java.util.Scanner;
import java.io.File;
import java.nio.file.*;
import java.lang.StringBuilder;

public class Main
{
    public static String name;
    public static String originalname;
    public static void main (String[] args){
        Scanner scanny = new Scanner(System.in);
        System.out.print("Want to hear your own personal waltz in the style of Mozart?\n");
        System.out.print("This is your own waltz bounded to your name. It is different from\n");
        System.out.print("anybody else's waltz. There are 1.3*10^29 different possible\n");
        System.out.println("possibilities and this is yours. Enter your name to proceed:\n");
        name = scanny.nextLine();
        originalname = name;
        name = name.toLowerCase();
        System.out.println("\nHold on just a second while your piece is being generated...");
        String reversedname = new StringBuilder(name).reverse().toString();
        while(name.length() < 32){
            name += reversedname;
            name += originalname;
        }
        char[] letters = name.toCharArray();
        int[] numbers = new int[32];
        for(int i = 0; i < 32; i++){
            numbers[i] = (int) letters[i] - 96;
        }
        /*
        for(int i : numbers){
        System.out.println(i);
        }
         */
        int[][] minuet = new int[][]{
                { 96, 22, 141, 41, 105, 122, 11, 30, 70, 121, 26, 9, 112, 49, 109, 14 },
                { 32, 6, 128, 63, 146, 46, 134, 81, 117, 39, 126, 56, 174, 18, 116, 83},
                { 69, 95, 158, 13, 153, 55, 110, 24, 66, 139, 15, 132, 73, 58, 145, 79},
                { 40, 17, 113, 85, 161,  2, 159, 100, 90, 176,  7, 34, 67, 160, 52, 170},
                { 148, 74, 163, 45, 80, 97, 36, 107, 25, 143, 64, 125, 76, 136, 1, 93},
                { 104, 157, 27, 167, 154, 68, 118, 91, 138, 71, 150, 29, 101, 162, 23, 151},
                { 152, 60, 171, 53, 99, 133, 21, 127, 16, 155, 57, 175, 43, 168, 89, 172}, 
                { 119, 84, 114, 50, 140, 86, 169, 94, 120, 88, 48, 166, 51, 115, 72, 111},
                { 98, 142, 42, 156, 75, 129, 62, 123, 65, 77, 19, 82, 137, 38, 149, 8},
                { 3, 87, 165, 61, 135, 47, 147, 33, 102, 4, 31, 164, 144, 59, 173, 78},
                { 54, 130, 10, 103, 28, 37, 106,  5, 35, 20, 108, 92, 12, 124, 44, 131},
            };

        int[] chosenminuet = new int[16];

        int[][] trio = new int[][]{
                { 72, 6, 59, 25, 81, 41, 89, 13, 36, 5, 46, 79, 30, 95, 19, 66},
                { 56, 82, 42, 74, 14, 7, 26, 71, 76, 20, 64, 84, 8, 35, 47, 88},
                { 75, 39, 54, 1, 65, 43, 15, 80, 9, 34, 93, 48, 69, 58, 90, 21},
                { 40, 73, 16, 68, 29, 55, 2, 61, 22, 67, 49, 77, 57, 87, 33, 10},
                { 83, 3, 28, 53, 37, 17, 44, 70, 63, 85, 32, 96, 12, 23, 50, 91},
                { 18, 45, 62, 38, 4, 27, 52, 94, 11, 92, 24, 86, 51, 60, 78, 31},
            };

        int[] chosentrio = new int[16];
        for(int i = 0; i < 16; i++){
            Random randy = new Random(numbers[i]);
            //System.out.println(randy.nextInt(11)+1);
            chosenminuet[i] = minuet[randy.nextInt(11)][i];
        }
        //print(chosenminuet);
        WavAppender.splice("./AudioFiles/"+"M"+chosenminuet[0]+".wav","AudioFiles/"+"M"+chosenminuet[1]+".wav","./Temp/"+0+".wav");
        for(int i = 0; i<15; i++){
            WavAppender.splice("./Temp/"+(i)+".wav","./AudioFiles/"+"M"+chosenminuet[i+1]+".wav","./Temp/"+(i+1)+".wav");
        }

        for(int i = 0; i < 16; i++){
            Random randy = new Random(numbers[i]*numbers[i]);
            //System.out.println(randy.nextInt(11)+1);
            chosentrio[i] = trio[randy.nextInt(6)][i];
        }
        //print(chosentrio);

        WavAppender.splice(".\\AudioFiles\\"+"T"+chosentrio[0]+".wav",".\\AudioFiles\\"+"T"+chosentrio[1]+".wav",".\\Temp\\"+16+".wav");
        for(int i = 16; i<31; i++){
            WavAppender.splice(".\\Temp\\"+(i)+".wav",".\\AudioFiles\"+"T"+chosentrio[i+1-16]+".wav",".\\Temp\\"+(i+1)+".wav");
        }
        WavAppender.splice(".\\Temp\\15.wav",".\\Temp\\31.wav",".\\Temp\\Complete.wav");

        System.out.println("\nWould you like to save this piece? Enter true or false");
        JukeBox juke = new JukeBox(".\\Temp\\Complete.wav");
        juke.play();

        boolean trip = false;
        boolean shouldSave = false;
        while(!trip){
            try{
                Scanner yeet = new Scanner(System.in);
                shouldSave = yeet.nextBoolean();
                trip = true;
            } catch (Exception e){
                System.out.println("\fPlease enter true if you would like to save your piece.\nOr, enter false to end this session");
            }
        }

        if(shouldSave){
            File src = new File(".\\Temp\\Complete.wav");
            String Filename="..\\SavedAudio_"+originalname+".wav";
            File target = new File(Filename);
            System.out.println("this is the path your saving to: "+ Filename);

            try{ //https://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java
                Files.copy(src.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
                System.out.println("File saved succesfully! Find it in the SavedAudio folder. See ya later");
            } catch (Exception e) {System.out.println("Copying failed. Try again or contact Aaron.\nThis is likely the result of you using any of the following characters in your name \\ / : ?  \" < > | ");}
        }
        else{
            System.out.println("\nSee ya!");
        }

        //This tries to clear up some space but it does not always work. It tries to delete the temporary files.
        for(int i = 0; i<31; i++){
            File file = new File(".\\Temp\\"+i+".wav");
            file.delete();
            //System.out.println(i+":"+file.delete());
        }
    }

    public static void print(int[] yeet){
        System.out.print("{");
        for(int x: yeet){
            System.out.print(x+", ");
        }
        System.out.println("}");
    }
}

看看我如何把。\\放在文件前面以使该路径相对,现在要小心,这只适用于windows,因为其他操作系统可能不同,最好的方法是从中获取url路径资源,你应该能够在其他答案的堆栈溢出中找到它