Reputation: 433
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):
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!
Upvotes: 0
Views: 359
Reputation: 151
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.
Upvotes: 1
Reputation: 5811
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
Upvotes: 1
Reputation: 122364
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
.
Upvotes: 3