command-line main() classpath etc.

Discussion in 'Java' started by bb, Jan 10, 2006.

  1. bb

    bb Guest

    I have a class file with a main method in the following directory:
    C:\tea\emat\web\WEB-INF\classes\emat\com\edi
    The name of the file is: Edi210Parser. The class has a main method.
    I've developed and tested the file under Eclipse, which has shielded me

    from the classpath and command-line args.


    I've defined the file in the Windows classpath setting as:
    C:\tea\emat\web\WEB-INF\classes\emat\com\edi;


    For the life of me I can't remember how to run the file from a Windows
    command-line. I've tried the following:
    c:>java -classpath emat\com\edi\Edi210Parser out.txt (to read the input

    file that exists in the same directory)


    I've changed directories to the place where the file is:
    C:\tea\emat\web\WEB-INF\classes\emat\com\edi and run:
    c:\tea\emat\web\WEB-INF\classes\emat\com\edi>java Edi210Parser out.txt
    and
    c:\tea\emat\web\WEB-INF\classes\emat\com\edi>java
    emat.com.edi.Edi210Parser out.txt


    The closest I've come to getting it right is:
    c:\tea\emat\web\WEB-INF\classes\emat\com\edi>java -classpath
    EdiParser210 out.txt
    which results in:
    Exception in thread "main" java.lang.NoClassDefFoundError: out/txt,
    which makes no sense at this point but shows (or seems to) that the
    class was found.


    Any help would be appreciated....
     
    bb, Jan 10, 2006
    #1
    1. Advertising

  2. bb

    Eric Sosman Guest

    bb wrote On 01/10/06 18:00,:
    > I have a class file with a main method in the following directory:
    > C:\tea\emat\web\WEB-INF\classes\emat\com\edi
    > The name of the file is: Edi210Parser. The class has a main method.
    > I've developed and tested the file under Eclipse, which has shielded me
    >
    > from the classpath and command-line args.
    >
    >
    > I've defined the file in the Windows classpath setting as:
    > C:\tea\emat\web\WEB-INF\classes\emat\com\edi;
    >
    >
    > For the life of me I can't remember how to run the file from a Windows
    > command-line. I've tried the following:
    > c:>java -classpath emat\com\edi\Edi210Parser out.txt (to read the input
    >
    > file that exists in the same directory)


    The crucial piece of information you've omitted is
    the name of the class whose main() method you're trying
    to run. Yes, I know you said "Edi210Parser" -- but
    that's only the least-significant piece of the name:
    you've not divulged the package portion.

    I'm going to guess that Edi210Parser.java begins
    with "package com.edi;" so that the full name of the
    class is "com.edi.Edi210Parser" (make adjustments as
    necessary for whatever the actual package name is).
    Point #1, then, is that com.edi.Edi210Parser is the
    name you must supply on the java command line; Java is
    not a mind-reader, able to pluck the package name out
    of thin air.

    Once you've done that much, Java will look for the
    class by starting where the classpath ends (roughly
    speaking) and looking for com\edi\Edi210Parser.class.
    See the correspondence between the package name and the
    directory structure? Good. So your classpath should
    be set to c:\tea\emat\web\WEB-INF\classes\emat, because
    that's where you want the search for com\... to begin.

    Finally, setting the classpath and specifying the
    full class name doesn't change your current directory
    in any way. If you want "out.txt" to be found in some
    directory, you need to cd to that directory before you
    start Java. Putting it all together:

    cd directory-where-out.txt-lives
    java -classpath c:\tea\emat\web\WEB-INF\classes\emat
    com.edi.Edi210Parser out.txt

    (The final two lines are really just one, wrapped for
    legibility.)


    Adjust as needed for your actual structure -- for
    example, if the package is emat.com.edi, you'd set the
    classpath one level higher and prepend "emat." to the
    package name on the command line.

    --
     
    Eric Sosman, Jan 10, 2006
    #2
    1. Advertising

  3. bb

    James Westby Guest

    bb wrote:
    > I have a class file with a main method in the following directory:
    > C:\tea\emat\web\WEB-INF\classes\emat\com\edi
    > The name of the file is: Edi210Parser. The class has a main method.
    > I've developed and tested the file under Eclipse, which has shielded me
    >
    > from the classpath and command-line args.
    >
    >
    > I've defined the file in the Windows classpath setting as:
    > C:\tea\emat\web\WEB-INF\classes\emat\com\edi;
    >
    >
    > For the life of me I can't remember how to run the file from a Windows
    > command-line. I've tried the following:
    > c:>java -classpath emat\com\edi\Edi210Parser out.txt (to read the input
    >
    > file that exists in the same directory)
    >
    >
    > I've changed directories to the place where the file is:
    > C:\tea\emat\web\WEB-INF\classes\emat\com\edi and run:
    > c:\tea\emat\web\WEB-INF\classes\emat\com\edi>java Edi210Parser out.txt
    > and
    > c:\tea\emat\web\WEB-INF\classes\emat\com\edi>java
    > emat.com.edi.Edi210Parser out.txt
    >
    >
    > The closest I've come to getting it right is:
    > c:\tea\emat\web\WEB-INF\classes\emat\com\edi>java -classpath
    > EdiParser210 out.txt
    > which results in:
    > Exception in thread "main" java.lang.NoClassDefFoundError: out/txt,
    > which makes no sense at this point but shows (or seems to) that the
    > class was found.
    >
    >
    > Any help would be appreciated....
    >


    If your main class is in package com.edi then you need to be in the
    directory above this, so


    C:\tea\emat\web\WEB-INF\classes\emat\

    then run

    java -classpath . com.edi.EdiParser210 out.txt

    Though the -classpath is probably un-needed. If your package is
    different then adjust accordingly.


    James
     
    James Westby, Jan 10, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Flip
    Replies:
    3
    Views:
    7,279
    Tony Morris
    Feb 9, 2004
  2. Gabe
    Replies:
    3
    Views:
    1,078
  3. Replies:
    2
    Views:
    755
  4. Replies:
    2
    Views:
    1,112
    Mike Schilling
    Mar 2, 2008
  5. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    413
    Fredrik Lundh
    Aug 13, 2008
Loading...

Share This Page