0

I am learning Java, and I have been programming an 80s-styled text based game. I have experience with coding so I know I will be done soon. Just so I knew how, I decided to make a .jar file. It created fine, but I have a problem: I want it to run and work in the command prompt, just like if I were using the "java FileName" command. Is there any way to do this? I want to distribute it so that for Windows, it runs in the command prompt exclusively (Apple and Linux is down the line).

Edit/Summary: To some people, this might be a duplicate of a question. It's not for these simple reasons: I know how to create a .jar file. I am trying to figure out how to create a specific type of .jar file that opens and runs in the command prompt.

3 Answers3

2

I assume that you have compiled your .java files into class files. If so, than do the following:

  1. Make a manifest file called manifest.txt and inside the file mention the class name where the main class resides. For example:

Main-Class: com.A.B.MyMainClass

com.A.B.MyMainClass is example here. You should mention your package names. Put the above line in the manifest.txt file

  1. Than, suppose you have your class files in c:\test\classes\ folder and suppose your class files in this folder are a.class and b.class and MyMainClass.class

Now execute the command:

jar -cvfm MyExecutable.jar manifest.txt c:/test/classes/*.class

It will create an exectable jar for you which you can run in Windows and Mac and also in Linux.

To execute the jar file:

java -jar MyExecutable.jar in the command prompt.

Mahbub Rahman
  • 1,295
  • 1
  • 24
  • 44
  • Thank you! I failed to realize that I hadn't set my main class. I'll test it in the morning. As for now, many thanks! –  Jun 14 '16 at 03:54
1

Use this to run your jar on command line:

java -jar <jar-name>.jar

Assuming that you have java installed on the system

Slartibartfast
  • 1,592
  • 4
  • 22
  • 33