For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2008 > javac classpath; where to compile from?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author javac classpath; where to compile from?
Thufir

2008-03-20, 8:19 am

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

2008-03-20, 8:19 am

>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
--

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

2008-03-20, 7:23 pm

Thufir wrote:

> 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

2008-03-20, 7:23 pm

On Thu, 20 Mar 2008 09:46:40 -0700, Mark Space wrote:


> 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

Thufir

2008-03-20, 7:23 pm

On Thu, 20 Mar 2008 12:50:34 +0000, Roedy Green wrote:


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



What about ruby? or raven?



-Thufir

Mark Space

2008-03-20, 7:23 pm

Thufir wrote:

> 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?
Roedy Green

2008-03-20, 10:18 pm

On Thu, 20 Mar 2008 19:50:43 GMT, Thufir <hawat.thufir@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>What about ruby? or raven?


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

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

2008-03-20, 10:18 pm

Thufir wrote:
> 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
>


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.

--

Knute Johnson
email s/nospam/linux/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDem
Thufir

2008-03-21, 4:43 am

On Thu, 20 Mar 2008 23:36:59 +0000, Mark Space wrote:


> 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

thufir

2008-03-28, 7:31 pm

On Thu, 20 Mar 2008 19:10:08 -0700, Knute Johnson wrote:

> 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

2008-03-28, 10:24 pm

Thufir wrote:

Knute Johnson wrote:[color=darkred]
> 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/t...ac.html#options>

--
Lew
Mark Space

2008-03-29, 4:51 am

Lew wrote:

> <http://java.sun.com/javase/6/docs/t...ac.html#options>
>
>


One small point:

<http://java.sun.com/javase/6/docs/t...dows/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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com