1

I thoroughly checked this one, and the standard answers aren't working for me.

Whenever I try to execute a jar file from cmd prompt, instead of opening the jar file it opens a new window of Dr. Java.

I've used DrJava's "Create custom drjava jar" to create a jar file called "TestJar.jar", including:

Manifest.txt:

Main-Class: com.package.name.TestJar Class-Path: algs4.jar

TestJar.java:

public class TestJar { public static void main(String[] args) { System.out.println("Hello"); StdOut.println("hello StdOut"); } } 

I type this in the cmd prompt:

java -jar TestJar.jar 

And DrJava opens a new window. Any ideas? I've tried creating the jar file from cmd prompt but all commands I type starting with "jar" get this error:

'jar' is not recognized as an internal or external command, operable program or batch file.

Any help would be greatly appreciated...I'm pretty new to Java.

4
  • With the standard JRE, you need syntax like java -jar TestJar.jar TestJar. ALSO: If "jar is not recognized...", then it sounds like you need to add your Java directory to the %PATH% ;) Commented Jul 1, 2013 at 22:48
  • Also make sure to have Main-Class and Class-Path on separate lines. And don't forget to press an enter after the last line in manifest file (to get a proper EOL). Commented Jul 2, 2013 at 0:15
  • @paulsm4 Using the syntax java -jar TestJar.jar TestJar it doesn't do anything...no error messages, but nor does it open an instance of DrJava either. All the main method is doing is system.out.println(). I should see that in the command prompt, right? Commented Jul 2, 2013 at 1:03
  • @Usman, thanks but I did those things. The commenter here doesn't save line breaks. Commented Jul 2, 2013 at 1:04

3 Answers 3

1

The problem you're having is that the jar program is not in your %PATH%. You may find this answer helpful.

Sign up to request clarification or add additional context in comments.

7 Comments

I've tried adding various things to the PATH variable. But I actually can't find the jar program on my computer. Is it jar.exe?
@EricWalton Yes, it's jar.exe. Do you know where java.exe is? The jar.exe program should be in the same folder.
java.exe is also placed in C:\Windows\System32, if I recall correctly, but there is a java.exe in the installation directory's bin folder. Try looking for a folder like C:\Program Files\Java\jdk_[version]\bin. It would be in Program Files (x86) if you have the 32 bit version.
the jar program is part of the JDK, not the JRE, and it's used only to create / modify / test jars, not to run them.
Thanks, this ended up being part of the solution. Eventually was able to point to teh jar from cmd line, but only after setting the path variable.
|
0

Java determines the location of the main function using a manifest. If you unzip your jar file, you'll see the manifest file in a META-INF folder. (A jar file is just a zip file with a different extension and some specific files inside.)

You'll need an entry like this in your manifest:

Main-Class: packagename.classname 

See here for more details about setting the location of main.

To control the manifest in Dr. Java, I think you will need to create a custom manifest. This source provides these instructions:

For more control over the properties of the jar, you may enter a custom manifest by selecting "Custom Manifest" and pressing the "Edit Manifest" button.

3 Comments

Here's the manifest file I included: Main-Class: com.package.name.TestJar Is that the wrong syntax? I'm not sure what the package name would be.
You have to declare the package and set up the folder structure to match: docs.oracle.com/javase/tutorial/java/package/packages.html So if you want your code in a package named mypack, create a folder named mypack, put the TestJar.java code file inside the folder, and put the statement package mypack at the very top of the code file. Non-packaged code files are unsupported in Java 7. Then your manifest should have Main-Class: mypack.TestJar. You may have to fiddle around with Dr. Java to make the package work, but the mypack folder should be alongside META-INF in the jar file.
I just tried that. it didn't make a difference. What fiddling with DrJava do you suggest?
0

1) To run your test jar from a Windows command:

a) Make sure Java is installed on your PC

b) Set your %JAVA_HOME% (EX: set JAVA_HOME=c:\Program files\java\jre7)

c) Update your %PATH% (EX: PATH=c:\Program files\java\jre7\bin;%PATH%)

d) Invoke program with "java -jar JARFILE MAIN-CLASS (EX: java -jar TestJar.jar TestJar)

2) To run DrJava .jar on Windows, I would just double-click on the .jar file from Windows Explorer

3) To test and debug your program, I would just use DrJava (instead of from the command line).

4) You do NOT need "jar.exe" installed to run a .jar file. You only need it to view, modify or create a .jar. jar.exe comes with the Java JDK.

5) I suspect you probably WILL need the Java JDK to build or debug any programs with DrJava, however.

Here's a good link on running .jar files from Windows, if you need more help:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.