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 / First Aid / March 2008

Tip: Looking for answers? Try searching our database.

javac classpath; where to compile from?

Thread view: 
Thufir - 20 Mar 2008 11:41 GMT
I would like to compile and execute (run) without changing directories.  
Currently, however, I get the following error:

thufir@arrakis:~/java$
thufir@arrakis:~/java$ javac -cp src -d /home/thufir/java/build/ src/com/
web_tomorrow/CPTest2.java
thufir@arrakis:~/java$
thufir@arrakis:~/java$ java build.com.web_tomorrow.CPTest
Exception in thread "main" java.lang.NoClassDefFoundError: build/com/
web_tomorrow/CPTest
thufir@arrakis:~/java$
thufir@arrakis:~/java$

To get good output, only this seems to work:

thufir@arrakis:~/java/src$
thufir@arrakis:~/java/src$ javac -cp . -d /home/thufir/java/build/ com/
web_tomorrow/CPTest2.java
thufir@arrakis:~/java/src$
thufir@arrakis:~/java/src$ cd ..
thufir@arrakis:~/java$
thufir@arrakis:~/java$ cd build/
thufir@arrakis:~/java/build$
thufir@arrakis:~/java/build$ java com.web_tomorrow.CPTest2
Run CPTest2.main()
thufir@arrakis:~/java/build$
thufir@arrakis:~/java/build$

how can I do this without changing directories?

thanks,

Thufir
Roedy Green - 20 Mar 2008 13:50 GMT
>how can I do this without changing directories?

nowadays people do this with ant, not bat files.  See
http://mindprod.com/jgloss/ant.html

If you want to do it the hard way, see
http://mindprod.com/jgloss/classpath.html
http://mindprod.com/jgloss/javacexe.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Thufir - 20 Mar 2008 20:50 GMT
> nowadays people do this with ant, not bat files.  See
> http://mindprod.com/jgloss/ant.html

What about ruby?  or raven?

-Thufir
Roedy Green - 21 Mar 2008 02:04 GMT
>What about ruby?  or raven?

ant is a specialised tool for doing builds. Ruby is a general purpose
language.  
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Mark Space - 20 Mar 2008 17:46 GMT
> thufir@arrakis:~/java$ java build.com.web_tomorrow.CPTest

I think this is the problem here.  The package name for your class is
not build.com.web_tomorrow .  It's just com.web_tomorrow.  Try setting
the class path to build and executing the class.  Maybe:

java -cp ./build com.web_tomorrow.CPTest

You may have other errors if you are relying on the classpath from the
environment.  You'll have to add those paths to the command line too if
you are using the CLASSPATH variable.
Thufir - 20 Mar 2008 20:50 GMT
> I think this is the problem here.  The package name for your class is
> not build.com.web_tomorrow .  It's just com.web_tomorrow.  Try setting
> the class path to build and executing the class.

Exactly, the package is com.web_tomorrow as per:

http://www.kevinboone.com/classpath.html

It wouldn't make sense to have a package of build.com.web_tomorrow, or
would it?  Sounds odd to me, I thought the convention was for packages to
begin with com,net,org, etc (where possible).

-Thufir
Mark Space - 21 Mar 2008 00:36 GMT
> It wouldn't make sense to have a package of build.com.web_tomorrow, or
> would it?  Sounds odd to me, I thought the convention was for packages to
> begin with com,net,org, etc (where possible).

Or they begin with java.  Or javax.  Or anything that doesn't collide
with some other package name. ;-)

Btw, did you get your java runtime issue working?
Thufir - 21 Mar 2008 08:38 GMT
> Or they begin with java.  Or javax.  Or anything that doesn't collide
> with some other package name. ;-)
>
> Btw, did you get your java runtime issue working?

Yes; it was just a variation of this problem.

-Thufir
Knute Johnson - 21 Mar 2008 03:10 GMT
> I would like to compile and execute (run) without changing directories.  
> Currently, however, I get the following error:
[quoted text clipped - 29 lines]
>
> Thufir

You can't classpath your way out of this problem.  You must be in a
directory that is above the packages you wish to access.  You can't
point your classpath at source files only .class files and .jars.

I've never used ant (I'll have to look into that) but making a simple
batch file is very easy.  For development I make a compile batch file
and a run batch file so I just have to type a couple of keystrokes after
I edit my program.

Signature

Knute Johnson
email s/nospam/linux/

     ------->>>>>>http://www.NewsDem

thufir - 28 Mar 2008 23:25 GMT
> You can't classpath your way out of this problem.  You must be in a
> directory that is above the packages you wish to access.

Thanks,

Thufir
Lew - 29 Mar 2008 03:50 GMT
Thufir wrote:
>> I would like to compile and execute (run) without changing
>> directories.  Currently, however, I get the following error:
>> ...
>> how can I do this without changing directories?

> You can't classpath your way out of this problem.  You must be in a
> directory that is above the packages you wish to access.  

Actually, that isn't necessary.

> You can't point your classpath at source files only .class files and .jars.

However, there is an option to javac to do exactly that:
> -sourcepath sourcepath
> Specify the source code path to search for class or interface definitions.
> As with the user class path, source path entries are separated by colons (:)
> and can be directories, JAR archives, or ZIP archives. If packages are used,
> the local path name within the directory or archive must reflect the package name.
<http://java.sun.com/javase/6/docs/technotes/tools/solaris/javac.html#options>

Signature

Lew

Mark Space - 29 Mar 2008 06:04 GMT
>> -sourcepath sourcepath
>> Specify the source code path to search for class or interface
[quoted text clipped - 3 lines]
>> directory or archive must reflect the package name.
> <http://java.sun.com/javase/6/docs/technotes/tools/solaris/javac.html#options>

One small point:

<http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html>

-sourcepath sourcepath
    Specify the source code path to search for class or interface
definitions. As with the user class path, source path entries are
separated by semicolons (;) and can be directories, JAR archives, or ZIP
archives. If packages are used, the local path name within the directory
or archive must reflect the package name.

Note the Windows versions use a ; rather than : like Unix and Solaris


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



©2008 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.