We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Compiling multiple java classes in the dame directory in Windows XP command line

polkadot
polkadot asked
on
Medium Priority
170 Views
Last Modified: 2013-11-23
I have two classes one that uses the oher. The first file compiles without any problem, the second gives a compiler error that it doesn't not recognize the object type which has been defined in the first file.

The two files are in the same folder/directory, what am I doing wrong?


something like this

file 1:
------
public class boo
{
   public boo(int x)
...

}

file 2:
------
class usesboo
{
    boo x;
    public usesboo(int y)
...

}
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
usesboo needs to be public

Author

Commented:
yes it is public, that was a typo

Author

Commented:
** correction

file 2:
------
public class usesboo
{
    boo x;
    public usesboo(int y)
...

}
CERTIFIED EXPERT
Top Expert 2016

Commented:
Try

javac *.java

Author

Commented:
still getting "cannot find symbol" ... I've checked and rechecked spelling of the classes and made sure everything is public
CERTIFIED EXPERT
Top Expert 2016

Commented:
Can you post your *actual* code?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> still getting "cannot find symbol" ... I've checked and rechecked spelling of the classes and made sure everything is public

which line are you getting that
and what is the compete error

Author

Commented:
getting "cannot find symbol" in file 2 on  statement boo x;

Sorry, I was wrong it does comiple with javac *.java with no problems... but is there any way to get the files to compile independently?

Id like to compile file1 first, then file2 ..
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Id like to compile file1 first, then file2 ..

Why so? You'd have to calculate their dependencies yourself and compile them in order (more effort than it's worth ;-))
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
should work:

javac -classpath . boo.java
javac -classpath . usesboo.java

(shouldn't actuially need the classpath)

Author

Commented:
Okay, everything compiled and I got classes from all my java files now. There is one class with a main method MainProg.java and I'm trying to run it with the command ...

c:\java MainProg

Exception in thread main ... no classdef found error : MainProg


MainProg.class is in the same directory as the other java files and classes ... does this need a classpath too?

CERTIFIED EXPERT
Top Expert 2016

Commented:
try

java -cp MainProg
CERTIFIED EXPERT
Top Expert 2016

Commented:
Sorry -typo there:


java -cp . MainProg

Author

Commented:
Okay it worked, but I don't remeber ever having to do this before on another system, why is it so differnt now?
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
If you clear yuor classpath it will work without that
if you have no classpath then . it used by default

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Manikandan ThiagarajanSenior consultant

Commented:
>>If you clear yuor classpath it will work without that.

set classpath=

this command is used to unset the classpath

Top Expert 2006

Commented:
if YourClass is in a package for example package test and you are already in C:\test
you'll be able to compile your code with "javac YourClass"
but you'll get a NoClassDefFoundError if you try "C:\test>java YourClass" within test folder
you need to get out of test (you are then in C:\) and then "C:\>javac test.YourClass"
or if you a were in D:\ you would use "D:\>java -cp D:\ test.A"
Top Expert 2006

Commented:
typo last line should be "D:\>java -cp C:\ test.YourClass "
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
BTW, have you heard of Ant?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.