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.

Eric Walton
  • 27
  • 1
  • 6
  • 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% ;) – paulsm4 Jul 01 '13 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). – Usman Saleem Jul 02 '13 at 00: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? – Eric Walton Jul 02 '13 at 01:03
  • @Usman, thanks but I did those things. The commenter here doesn't save line breaks. – Eric Walton Jul 02 '13 at 01:04

3 Answers3

1

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

Community
  • 1
  • 1
sigpwned
  • 6,057
  • 3
  • 23
  • 44
  • 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? – Eric Walton Jul 01 '13 at 23:35
  • @EricWalton Yes, it's `jar.exe`. Do you know where `java.exe` is? The `jar.exe` program should be in the same folder. – sigpwned Jul 01 '13 at 23:58
  • `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. – jpmc26 Jul 02 '13 at 00:05
  • Thanks, guys, but I stil can't find it. I found two paths that look promising: C:/Program Files/Java/jre6/bin and C:/Program Files (x86)/Java/jre7/bin Both have the java application, neither has the jar.exe file. Do I need to reinstall something? I'd hate to, as everything else with java works fine. – Eric Walton Jul 02 '13 at 00:22
  • 1
    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. – Ian McLaird Jul 02 '13 at 19:28
  • 1
    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. – Eric Walton Jul 07 '13 at 08:17
  • Glad it was helpful! I appreciate your coming back to this question many days later and accepting an answer. :) – sigpwned Jul 07 '13 at 14:44
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.

jpmc26
  • 23,237
  • 9
  • 76
  • 129
  • 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. – Eric Walton Jul 02 '13 at 00:28
  • You have to declare the package and set up the folder structure to match: http://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. – jpmc26 Jul 02 '13 at 05:50
  • I just tried that. it didn't make a difference. What fiddling with DrJava do you suggest? – Eric Walton Jul 02 '13 at 12:32
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:

paulsm4
  • 99,714
  • 15
  • 125
  • 160