.jar file won't run? (I feel like I'm missing something obvious, looked to solutions myself before coming here.)

StackOverflow https://stackoverflow.com/questions/22503804

  •  17-06-2023
  •  | 
  •  

Question

I wanted to find out how to create an executable .jar file, so I wrote a small sample program to test it. This sample program takes some default text and turns it into some code based on the Caesar code. Anyway, it outputs normally when run in Eclipse.

I doubt this helps, but here's the source code for the two java classes.

Main.java

public class Main {
    public static void main (String [] args){
    Caesar caesarCode = new Caesar();
    caesarCode.setDecodedText("this");
    caesarCode.setShiftPos(3);

    String cipherText = caesarCode.deencode(caesarCode.getDecodedText(), caesarCode.getShiftPos());
    System.out.print(cipherText+"\n");

    String plainText = caesarCode.deencode(cipherText, caesarCode.getShiftPos()*-1);
    System.out.print(plainText);
    }
}

Caesar.java

public class Caesar {
// global variables
String encodedText; //  encoded text
String decodedText; //  decoded text
int shiftPos;       //  shift positions to decode message

// getters and setters
public String getEncodedText() {
    return encodedText;
}

public void setEncodedText(String encodedText) {
    this.encodedText = encodedText;
}

public String getDecodedText() {
    return decodedText;
}

public void setDecodedText(String decodedText) {
    this.decodedText = decodedText;
}

public int getShiftPos() {
    return shiftPos;
}

public void setShiftPos(int shiftPos) {
    this.shiftPos = shiftPos;
}

public String deencode (String plainTextArg, int shiftPosArg) {
    // variables
    String plainText = plainTextArg;
    char[] cipherTextArray;
    String cipherText;
    int plainTextSize;
    int shiftPos = shiftPosArg;

    // initialize variables
    char[] plainTextArray = plainText.toCharArray();
    plainTextSize = plainTextArray.length;
    cipherTextArray = new char[plainTextSize];

    // shift cipher String by shiftPos
    for (int i = 0; i < plainTextSize; i++){
        cipherTextArray[i] = (char) (plainTextArray[i] + shiftPos);
    }

    cipherText = String.valueOf(cipherTextArray);

    // return cipher text
    return cipherText;
}
}

I created my .jar file this way (on a Mac):

  1. Created two directories on Desktop: "classes" and "source"
  2. Copied my two files (Caesar.java, Main.java) into the source folder.
  3. I compiled the files into .class files in the classes directory with the command "javac -d ../classes *.java"
  4. I created a manifest.txt file with the line "Main-Class: Main", and saved it as "manifest.txt" in the classes directory.
  5. I then created the .jar file with this command "jar -cvmf manifest.txt main.jar *.class".

The main.jar file was created successfully.

The problem is, when I ran it, nothing happened - no warnings, no popups, no error messages.

I'm thinking that it has something to do with the fact that it outputs to the terminal, but I can't wrap my head around it. I've also looked at many threads on this forum and others, but can't seem to see the problem. I'm going to experiment to see if it works for a GUI application in the meanwhile.

Greatly appreciate your help on this, thanks!

Was it helpful?

Solution

I'm thinking that it has something to do with the fact that it outputs to the terminal

If you want to see console output from your program then you will have to run it in the Terminal with java -jar main.jar. If you double click the JAR in the Finder or use the open command then it will run but any output will go to the system console log rather than being displayed directly. You can show system console messages by running syslog -C.

OTHER TIPS

In Windows, press Windows+R to invoke "Run..." dialog. Type cmd there and hit Enter. The Command Prompt window will appear (it's black).

At the prompt type cd %USERPROFILE%\Desktop\classes and hit Enter.

Then type command as suggested above: java -jar main.jar

It is possible to create executable jars from within eclipse if you want to. Right click on your project -> Export -> Java -> Runnable JAR file.

Note that You might need to run your jar file from the command line using java -jar <jarFile> to see System.out.println() and error messages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top