Results 1 to 17 of 17
  1. #1
    girishkumar is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default my code compiling but not running

    i am trying to run this simple code but this should be in package girish which is on desktop

    i want this program to work but package girish should be on desktop
    and error also is given below...

    i pass input through command line parameters


    ANY SUGGESTIONS



    package girish;



    class Greeter
    {


    String name;

    Greeter(String n)
    {
    this.name=n;
    }

    public String sayHello()
    {
    String s="Hello";
    String f=s+" "+this.name;
    return f;
    }
    public static void main(String[] args)
    {

    Greeter g=new Greeter(args[0]);
    System.out.println(g.sayHello());
    }


    }





    Exception in thread "main" java.lang.NoClassDefFoundError: Greeter
    Caused by: java.lang.ClassNotFoundException: Greeter
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default

    The fully qualified name of your class is girish.Greeter and the class file should be stored in a directory "girish" somewhere. Also read about the classpath variable.

    kind regards,

    Jos

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    What JosAH said, and also, you need to run "java girish/Greeter <args>" and not "java girish/Greeter.class <args>".

    -Gary-

  4. #4
    girishkumar is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    my classpath file is set and class files comes to directory girish itself...
    but still not running


    y should i give full path like "java girish/Greeter" name when everything is set

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by girishkumar View Post
    my classpath file is set and class files comes to directory girish itself...
    but still not running


    y should i give full path like "java girish/Greeter" name when everything is set
    Suppose your directory "girish" is a subdirectory of directrory "X", so your class file can be found in /X/girish/Greeting.class. Your classpath variable is supposed to contain "X" when you want to run your class girish.Greeting. e.g. the following command will do the job:

    Java Code:
    java -cp /X girish.Greeting
    kind regards,

    Jos

  6. #6
    girishkumar is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    u mean to say when i run each class file i should give full path name like



    "java -cp C:\Documents and Settings\giri\Desktop\girish\Greeter"

  7. #7
    girishkumar is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    if i remove my package statement it works but i want to create packages and want to specify in java files..

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Read this:

    Managing Source and Class Files (The Java™ Tutorials > Learning the Java Language > Packages)

    And read the five pages leading up to that one too. Also, read again what JosAH said.

    -Gary-

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by girishkumar View Post
    u mean to say when i run each class file i should give full path name like



    "java -cp C:\Documents and Settings\giri\Desktop\girish\Greeter"
    No,I didn't mean that; your fully qualified class name is girish.Greeter; it can be found as a subdirectory of directory X, i.e. /X/girish/Greeter.class so your command line should look like this:

    Java Code:
    java -cp /X girish.Greeter
    kind regards,

    Jos

  10. #10
    girishkumar is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    @JoSAH and @gcalvin

    Thanku for ur support
    finally i understood it...

    thanku thanku thanku

  11. #11
    girishkumar is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    but i have to give java girish\Greeter to run ..
    what if i have hierarchy of packages like X/Y/Z then i have to give "java X/Y/Z/*.class"
    to run

    is there any shortcut to run

  12. #12
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by girishkumar View Post
    is there any shortcut to run

    sure.

    1. you can build a jar file Using JAR Files: The Basics (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files) so that you can start your java app with java -jar app.jar or

    1. you can create a shortcut from your os that executes "java X/Y/Z/app". put the shortcut on the desktop so you can start it with a doubleclick or in a directory in the systempath, so you can call your shortcut from everywhere. but remember: don't add the extension class to the classfile when you call java!


    any other suggestions?
    Last edited by j2me64; 03-15-2010 at 10:06 AM.

  13. #13
    girishkumar is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    @j2me64

    i just wanted to show i am running class file so i put class extension there

  14. #14
    girishkumar is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    You have two choices at this point: change directories up one level and try java test.PackTest, or add the top of your development class hierarchy to the CLASSPATH environmental variable. Then you will be able to use java test.PackTest from any directory, and Java will find the right .class file. For example, if you are working on your source code in a directory called C:\\myjava, then set your CLASSPATH to .;C:\\myjava;C:\\java\\classes


    Can anyone explain whats the second part exactly means..

    add the top of your development class hierarchy to the CLASSPATH environmental variable.

    pls if u can explain with respect to my example..
    my package is on desktop

  15. #15
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by girishkumar View Post
    add the top of your development class hierarchy to the CLASSPATH environmental variable.

    afaik: when you specify a subdirectory, you are NOT specifying the directories above it, so they will not be searched for classes. so if you want this, add the top of your directory to the CLASSPATH environmental variable or with -classpath <topofyourdirectory>. for detail, see my pm.
    Last edited by j2me64; 03-15-2010 at 10:37 AM.

  16. #16
    girishkumar is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    thanku j2me64 for pm


    kind regards
    GIRI

  17. #17
    girishkumar is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    ya good one..
    really good
    thanku for that

    ya u can delete it...

    kind reards
    GIRI

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2009, 10:58 AM
  2. Help me out in compiling the source code
    By aks.nitw in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2008, 08:33 AM
  3. Trouble compiling code
    By waelhelbawi in forum New To Java
    Replies: 1
    Last Post: 05-12-2008, 04:25 AM
  4. Compiling and running code in runtime
    By tim in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 06:58 PM
  5. Importing / compiling and running with .jar package
    By splinter64uk in forum New To Java
    Replies: 1
    Last Post: 12-05-2007, 02:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •