Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

command-line main() classpath etc.

Thread view: 
bb - 10 Jan 2006 23:00 GMT
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....
Eric Sosman - 10 Jan 2006 23:25 GMT
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
[quoted text clipped - 11 lines]
>
> 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.

Signature

Eric.Sosman@sun.com

James Westby - 10 Jan 2006 23:58 GMT
> I have a class file with a main method in the following directory:
> C:\tea\emat\web\WEB-INF\classes\emat\com\edi
[quoted text clipped - 28 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.