views:

100

answers:

4

Can anyone help me on how I have to run a java application which is in a package, from cmd? Please give me the necessay line that I have to type

Thank You


EDIT: (Copied a clarifying comment from one of the answers below)

No i mean a normal java aplication that belongs to a package, like

package x; class SampleOnly{ }

how you compile (and run) that file – Muhammed Yoosuf

A: 

If it is a jar that you are talking about then java -jar filename.jar

Bakkal
works only if the jar contains a manifest, and the manifest contains a Main-Class declaration.
aioobe
Right. The answer depends very much on what the OP means by "Java Application in a package".
Bakkal
+1  A: 

No i mean a normal java aplication that belongs 2 a package, like package x; class sampleOnly{ } hw u comple dat file

This little "bash session" should explain it:

$ ls .                   # Current directory contains the "x" package
x

$ ls x                   # The "x" package contains a Sample.java file...
Sample.java

$ cat x/Sample.java      # ...which looks like this.
package x;
public class Sample {
    public static void main(String... args) {
        System.out.println("Hello from Sample class");
    }
}

$ javac x/Sample.java    # Use "/" as delimiter and
                         # include the ".java"-suffix when compiling.

$ java x.Sample          # Use "." as delimiter when running, and don't include
                         # the ".class" suffix.
Hello from Sample class

$ 
aioobe
Opps diz method iz also not working!!!
Yoosuf
What error message do you get? Could you paste your entire bash-session on pastebin.org?
aioobe
package Sample;class Test{ public static void main(String args[]) { System.out.println("This class belongs to the package sample"); }}That is the code which I want to run from cmd, please show me how it should be done
Yoosuf
I thought that that was what I showed you :) Have a look at this: http://aioobe.org/run_java.mpeg
aioobe
+2  A: 

By package you mean a .jar file?

java [ options ] <class> [ arguments ... ]
java [ options ] -jar <file.jar> [ arguments ... ]
javaw [ options ] <class> [ arguments ... ]
javaw [ options ] -jar <file.jar> [ arguments ... ]

Running Java applications

AOI Karasu
no i mean a normal java aplication dat belongs 2 a package, likepackage x;class sampleOnly{}hw u comple dat file
Yoosuf
see my updated answer.
aioobe
A: 

And not to forget setting the main class and optional classpath in META-INF/MANIFEST.MF

Main-Class: org.example.HelloWorld
Class-Path: greetings.jar

Have a look to the JAR specs at http://java.sun.com/javase/6/docs/technotes/guides/jar/jar.html

Jan Goyvaerts
The OP does not seem to need help creating the jar, only running it.
aioobe