-2

I have created a nifty music generator thingy in Java. The code is not very efficient but it does run. I am using BlueJ by the way. BlueJ has a feature that is create an executable jar file where you select which class's main method to use and it will compile it. I keep on getting corrupt jar files on my computer and when I gave the program to a friend for him to do the same, but the jar file does nothing when clicked.

The program references music clip files (.wav) in the project folder but the jar file when decompiled does not have them.

It runs in the console and I just learned that the console does not work with Jar files. Fair enough. If I change this to JOptionPane for user input, will it show up? And how do I include the sound files too?

I am seeing some stuff about using the console but I do not have access to that on my school computer. Or at least I don't think I do. My end goal is to just be able to run a jar file and have it play. That way, I can run my jar file on my chromebook which is my main computer.

Edit: If you want to look at my code to see if I have an error, the project is here. https://drive.google.com/file/d/0B2a2yK5zF36HWHc0X1JzMU1ZRDA/view?usp=sharing

  • 4
    Who said console doesn't work with jar files?, you can run console based applications created with java in Linux, windows or Mac OS. Now try to ask one question at a time. For example: How to include wav files in my java project using BlueJ? or How to build executable jar files in BlueJ. Make sure you mention the operating system, what computers you use at your school? It looks to me you are using a Rasp Berry Pi but your not mentioning that. In windows the console is called the command prompt, in Linux and Mac OS they call it the terminal – AbelSurace May 24 '17 at 22:05
  • _If I change this to JOptionPane for user input, will it show up?_ I don't know. Why don't you try it. Please post your code IN the question. Thank you! –  May 24 '17 at 22:24
  • We are using windows 10 computers that are Dell I believe. They're big PC's, not laptops. BlueJ can make the jar file with the classes in them but it omits the necessary .wav files that are used in the program. I know that that is something I need to fix. I'm wondering though if the console (Like system.out.println) is another issue or is it just the wav files that are causing it not to work. – JesusLopez May 24 '17 at 23:05

2 Answers2

1

It's not very clear what you're asking (what error are you actually getting? why are *.wavs not included?) so you run the risk of getting the question closed. Still, I'll try to help you.

  • Jar file is nothing but a simple zip archive containing the compiled classes, resources (like your *.wav files), and a special MANIFEST.MF file
  • Any resources included in the jar itself can be used in the code, there's many tutorials on the topic but, in short, all you need to do is: InputStream wav = this.getClass().getResourceAsStream("sample.wav");
  • Yes, you can run jars just fine from a command line: java -jar file.jar
  • All you need to make a jar executable via the above command or a simple double-click is the Main-Class entry in your manifest, something akin to: Main-Class: com.example.ClassWithMain
  • Yes, creating a GUI application would work, your main method would then be in charge of creating the windows etc, but there's no relation between it being a GUI application and the jar being executable. Executing the jar just means running the main method from the class specified in the manifest.
  • The machine running the jar needs (at least) JRE installed. As said, a jar is just a zip archive of classes, not a native executable.
kaqqao
  • 12,984
  • 10
  • 64
  • 118
  • I have successfully implemented the sound files in my code but when BlueJ makes the Jar, they are not included within it. It's that and that the Jar does not open but I am not sure if that is why the jar does not open – JesusLopez May 24 '17 at 23:12
  • The project is a console application so double clicking the jar file will not work, you have to open the command prompt (type cmd in the windows search bottom right corner) then move to the folder where the jar file is "cd yourfolder" you have to know the path then from there type java - jar yourfilename.jar, change yourfolder and yourfile to your case. – AbelSurace May 25 '17 at 17:54
0

I think your problem can be divided in 2: One you probably are not including the *.wav files in your project, make sure you select the AudioFiles folder when creating your jar file:

enter image description here

Also the path to this files has to be relative to the location of your jar file as the application doesn't know from which location is being run, I modified your main class code to correct this. It runs fine on my computer, from the windows command window type "java -jar juke.jar" or whatever name you put to the jar file.

Modifieed Code:

    /**
 * 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("}");
    }
}

See how I put .\\ in front of the files to make that path relative, now be careful, this only works in windows, for other operating systems might be different, the best way is to get the url path from the resources, you should be able to find that here in Stack overflow from other answers

AbelSurace
  • 2,263
  • 1
  • 12
  • 16
  • That is weird. Maybe I will have to contact my school about updating BlueJ because I know that I did not have that option to check AudioFiles. Is there anything else you did to make that show up? Or is that a result of the modifications to the code? Also, I am very confused about the command prompt "java -jar juke.jar". So I am on my computer, and in BlueJ I exported the waltz to a jar file known as juke.jar So then I open CMD and type that in exactly as it is? Does juke.jar have to be anywhere specific? Or is a path needed there? This is all very new to me. – JesusLopez May 25 '17 at 01:48
  • I'm using BlueJ version 4.0.1, maybe you're using a different version, BlueJ is free, you can download the latest version, there is one that doesn't need installation and you just run the bluej.exe from folder go to https://www.bluej.org/ and select windows stand alone. I opened the project from zip file you shared using option open zip/jar. – AbelSurace May 25 '17 at 14:42
  • Now for the command yes you have to run it located in the same path as your jar file for example c:\yourdirectory>java -jar juke.jar. To go to command type cmd on the search box in windows to the left bottom corner then type cd yourdirectory and once your command is in the directory where you have the jar file Type the java -jar juke.jar. Ask your teacher for clarification if you don't understand my instructions and please accept the answer if it works for you. Cheers! – AbelSurace May 25 '17 at 14:42
  • I accepted because I got the Jar to open but i still am having some problems. I changed everything to joptionpane and also used eclipse. still having some issues with audio but ill make a new thread for that – JesusLopez May 26 '17 at 17:46