File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Compiling and running two source files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiling and running two source files" Watch "Compiling and running two source files" New topic
Author

Compiling and running two source files

Claude Cundiff
Ranch Hand

Joined: Mar 20, 2008
Posts: 78
Hello everyone!

I'm having some problems understanding how to use packages. I'd really appreciate any help you could provide.

I have two source files (Meaty.java, Classy.java) in directories like this:

C:\
---Java_Programs
-------com
----------test
--------------Meaty.java
--------------Classy.java

where Meaty calls Classy in its constructor
Here are the source files:


Here is my failed attempt to compile these via command prompt:

C:\Windows\system32>cd C:\Java_Programs
C:\Java_Programs>set CLASSPATH=C:\Java_Programs
C:\Java_Programs>javac -CLASSPATH com\test\*.java

This doesn't work at all.

I've done searches on how to do this. I've looked in all my java books but I'm just not getting this.

Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Claude Cundiff:
...Here is my failed attempt to compile these via command prompt:

C:\Windows\system32>cd C:\Java_Programs
C:\Java_Programs>set CLASSPATH=C:\Java_Programs
C:\Java_Programs>javac -CLASSPATH com\test\*.java
...

It looks like you're setting the classpath correctly (C:\Java_Programs), but then reseting it to nothing in the next line. Try invoking javac without the -CLASSPATH flag.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Claude Cundiff
Ranch Hand

Joined: Mar 20, 2008
Posts: 78
Tried the following:

C:\Java_Programs>javac com\test\*.java

'javac' is not recognized as an internal...

also, I checked the classpath with set CLASSPATH. It doesn't appear to be reset...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Claude Cundiff:
...'javac' is not recognized as an internal...

That suggests that your PATH is not updated. See step 4 of the installation instructions.
Claude Cundiff
Ranch Hand

Joined: Mar 20, 2008
Posts: 78
Thanks marc!!

I don't know what I'd do if it were not for your site!

If any newbies are reading this by some chance, when you run your compiled class, observe that the "\" becomes "/" - at least on Vista.

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Also note that if no classpath is specified, Java's default is to check the current directory. So unless you have an environment variable set for classpath (and it does not include a dot for the current directory), you shouldn't need to set classpath at all for this example.
Claude Cundiff
Ranch Hand

Joined: Mar 20, 2008
Posts: 78
marc,

Yeah, when I looked in my environment variables, I had
c:\Programs\Java\jdk1.6.0\bin

I forgot the _05 part.

So from what you are saying, if the path was just set to "." (parenthesis not included) then the whole classpath thing is not needed. So I need to do two things:
1.) set CLASSPATH = "" (set it to nothing) and
2.) change path = .; in the environment variable (or is it .*

Correct?

Again, thanks for your help.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I think that's a little confused. You should not set your CLASSPATH to nothing, and you should not set PATH to include "." (the current directory).

PATH tells your system where to find executables like javac and java. With this set, you can type "javac" at the command line, and your system will make sense of it. In addition to whatever is already there, your PATH should include...

c:\Programs\Java\jdk1.6.0_05\bin

...because that's where the Java executables are.

CLASSPATH tells Java where to find user-specified Java classes. The default is to use the current directory (.), so normally you don't need to do anything. If you set CLASSPATH to nothing, then you will not get the default behavior.

So (after correcting your CLASS variable) you should be able to just change the directory and compile...

C:\Windows\system32>cd C:\Java_Programs
C:\Java_Programs>javac com\test\*.java
Claude Cundiff
Ranch Hand

Joined: Mar 20, 2008
Posts: 78
Thanks marc, this is starting to make sense.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Compiling and running two source files
 
Similar Threads
Source: SCJP 5 Study Guide: Self Test Q. 12 - Page 798
SCJP 5 Study Guide: Self Test Q. 12 - Page 788
InvalidMappingException
Classpath, Packages, etc.
A pain in the ......Please help?