tags:

views:

106

answers:

4

I created a program that more or less holds an array of strings as an object and randomly prints one.

so basicaly

class Fun 
{


       string[] namestrings = new string[#] 

       constructor() 
       { fill with some strings} 

       public static void main (String[]arg) 
       { 
            create instance of class
            do some junk with it 
            method that prints it 
       } 
       method that prints it {} 
       another method 
    } 

when i compile and run it on the command line it works fine but when on the comand line i type in jar -cf Happy.jar Fun.class

i get a .jar file called Happy and when i click on it i get an error message that reads "the java Jar file happy could not be launched read the consol for possible error messages" I have a mac i'm running lepord if that makes a diference. Whats going on?

A: 

You don't need the .class file in your command. So:

java -jar Happy.jar Fun
anger
then i get an error that says Funn: no such file in derectory
David
Your class is Happy so you need to specify that as the class to run. Assuming your jar file is actually called Happy.jar this should do it.
anger
if i don't include Fun in the name how will it know to get information from the Fun.class file? also why doesn't it say you edited your post?
David
sorry my class name was fun. i got jumbled up in my head
David
my class name is fun. when i do jar -cf Fun.jar Fun i get the same error
David
try the edited version - you are running the wrong command (jar instead of java)
anger
when i do "java -jar Happy.jar Fun" this happens: "Unable to access jarfile Happy.jar"
David
+2  A: 

You can also add Main-Class attribute in your JAR's manifest. Then you can just do this

java -jar Happy.jar

See this.

Chuk Lee
well could you explain that?
David
You set a FQCN to the Main-Class attribute. Then you add the manifest to the jar file. When you do 'java -jar Happy.jar' to run your file, the VM will look at META-INF/manifest.mf to find out what is the class with the 'main' method and executes that. See the link above
Chuk Lee
+3  A: 

Have a manifest.mf with following content, and pack it to your JAR file as META-INF/manifest.mf

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 14.3-b01 (Sun Microsystems Inc.)
Main-Class: Fun

You may then execute your application using

java -jar Happy.jar

IDE will usually do that automatically for you, if you create your project as Java Application.

Yan Cheng CHEOK
+1  A: 

You need to tell the .jar file which class it will execute when run.

In your case sounds silly because you only have one class, but full featured java apps may have hundreds.

The way you do this is by adding an special file called manifest where you tell the jar, hey this is the class you should run.

So try this:

// HelloWold.java
// you know 
public class HelloWorld {
    public static void main( String [] args ) {
       System.out.printn("Look, I'm running");
    }
 }

Save it to HelloWorld.java

Compile it

$javac HelloWorld.java 

Create the manifest.mf file whit this content:

  Main-Class: HelloWorld

And the pack all together.

 $jar -cmf manifest.mf  yourJar.jar HelloWorld.class

A file named yourJar.jar is created.

Now if you double click on it, it will run, but it will disappear instantaneously, you can run it from the command like by typing:

 $java -jar yourJar.jar 

And the message will appear in the console.

I hope this helps.

OscarRyz