Hi everyone,

I really deserve a kick for asking this question so please bear with me.

Assume i have two classes class a and class b.
Now class b depends on class a to compile correctly. Class a has been compiled and the class file is generated correctly. Now when i compile class b, it says it can't find class a although class a is in the same directory as the source of class b.

This is what in class a

public class a
{

public void prt()
{
System.out.println("Jwenting is going to kill me for asking this question");
}

}

This is what is class b

public class b
{
public static void main(String args[])
{
a w = new a();
a.prt();
}     
}

Please note that both the sources of class a and class b are in the same directory and there are no packages involved.

Now this is how i am compiling class b

javac b.java

Is there any classpath setting that i need to take care of so that my class b can compile.

Again i apologize for my stupidy

Yours Sincerely

Richard West

You will need the current directory in your classpath either statically (through the CLASSPATH environment) or dynamically through the -classpath compiler flag.

And no, I'm not going to kill you. I'm all out of rope for the noose :)

Hi everyone,

I tried inputting these values into a string array and used the runtime class to compile my class. This is what the string array looks like

String[] str1 ={javac, "-classpath", ".", b.java};

I still get the error that it can't find class a although its clearly there

Richard West

public class b
{
public static void main(String args[])
{
a w = new a();
a.prt();
}

You're trying to call prt() as a static method of a, which it is not. Shouldn't that be w.prt()?

-Fredric

Hi everyone,

You're trying to call prt() as a static method of a, which it is not. Shouldn't that be w.prt()?

Yes you are right. That's a typo in my posting the code. The main problem still stands that's why i am baffled

Richard West

Ah, could it be you're not setting the compiler target and source paths?
. points to the directory you're executing the compiler from, which in your case is the location of the program you use to call the runtime compiler.

Hi everyone,

Ah, could it be you're not setting the compiler target and source paths?


Sorry but how do you set the above target and source paths. I have so far never run into this problem but a little snippet would help.

Richard West

-sourcepath compiler flag sets the root of the source directory
-d compiler flag sets the root of the destination directory.

and you should set your classpath to include that directory, not the current directory in your case (as that in your case is another one, the one from which you started your compiler wrapper program).

Hi everyone,

This is how i am compiling class a

C:\j2sdk1.4.2_04\bin\javac
C:\WINDOWS\Desktop\JPractice\compiler_test\a.java

I also tried compiling class a using the classpath flag

C:\j2sdk1.4.2_04\bin\javac -classpath .
C:\WINDOWS\Desktop\JPractice\compiler_test\a.java

Class a compiles with no errors using both ways of compilation

This is how i am compiling class b

C:\j2sdk1.4.2_04\bin\javac
C:\WINDOWS\Desktop\JPractice\compiler_test\b.java

I also tried compiling class b using the classpath flag

C:\j2sdk1.4.2_04\bin\javac -classpath .
C:\WINDOWS\Desktop\JPractice\compiler_test\b.java

class b is unable to compile both ways because the compiler complains that
it can't find class a

This is the full error listed by the compiler

C:\WINDOWS\Desktop\JPractice\compiler_test\b.java:6: cannot resolve
symbol
symbol  : class a 
location: class b
a w = new a();
^
C:\WINDOWS\Desktop\JPractice\compiler_test\b.java:6: cannot resolve
symbol
symbol  : class a 
location: class b
a w = new a();
          ^
2 errors

This is the location of my runtime wrapper program

C:\WINDOWS\Desktop\JPractice\JavaIDE_Projects\Swing\CompileMyPrograms.class

This is the source code of a.java

public class a
{

public void prt()
{
System.out.println("Class A test");
}

}

This is the source code of b.java

public class b
{
public static void main(String args[])
{
a w = new a();
w.prt();
}     
}

From the way i am compiling the above two separate files it can be seen
that both source files are in the same directory

I really hope someone could explain this weirdness

Yours Sincerely

Richard West

ps. Jwenting did i just get demoted from a guru poster to a junior member or am i dreaming

Ah, and there's your problem.
You need to add the root of your output directory (where your classes will go once compiled) to your classpath.

Thought you'd know that by now, you've been using Java for a while.

One more excellent example of why you should not use an IDE before you can dream the commandline :)

Hi everyone,

Ah, and there's your problem.
You need to add the root of your output directory (where your classes will go once compiled) to your classpath.

Yup you are right about this. I forgot about this because i have been using an ide for sometime

Thought you'd know that by now, you've been using Java for a while.

True but sometimes its the simplest things you forget.
Maybe age is catching up with me

One more excellent example of why you should not use an IDE before you can dream the commandline

True. I started with the command line but after using an ide for many years i forgot how to do certain things.

I wonder how long will it be before i forget where i live

Richard West

I've forgotten where I lived once in a while.
Could get there no problem, but couldn't remember the address or explain the route if my life depended on it.
Same with my phone number. I never call it, so I sometimes forget what it is.

Businesscards to the rescue :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.