Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to compile file which include "package" using javac

 
snow snow
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just help me ,here is the example :
package a.b;
public class Test1{
public void test(){
System.out.println("hello");
}
}
package a.c;
import a.b.Test1;
public class Test2{
public static void main(String args[]){
Test1 t1= new Test1();
T1.test()
}
}
i have compile Test1.java ---javac -d c:\jdk14\Test Test1.java
how can i complie Test2.java using javac ???
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
snow snow,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change to
t1.test();

It then should compile in the same way as your first file.
 
kobe bryan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to Barry Gaunt :
it doesn't work.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well T1.test(); will absolutely never work. The test method is not static, and therefore you need an instance to access it.

I think the problem is related to classpath and using the -d switch with javac. But The original poster doesn't give us any error output, so I'm only speculating.

Scenario:

You compile the first file. Fine. Where does the generated class file go? If you're leaving it in the same directory as your source files for both classes, then this is a problem.

There are two posts that talk about compiling trouble that might be relevant here. One is from the Java In General forum some time ago, and another is about servlets, but they both refer to using the -d switch.

Problem with Packages / Utility Classes in Servlets
(this one links to another thread.. it's worth a read as well).

ARGH! Package problem while using *
(the key to this one is in my second to last post, the 3rd last post in the thread. The paragraph that starts "In my case...". But don't just read from here; if you're still unsure after reading the whole thing, then focus on that paragraph.)
 
kobe bryan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfectly ,Mike Curwen .i think you have got snow's idea.
i have checked your list "url",and got the answer which were frustrating to me before,thanks .
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to Barry Gaunt :
it doesn't work.

Actually it does work, and I know because I took your files, corrected them, compiled them, and run the program.

-Barry
[ February 21, 2003: Message edited by: Barry Gaunt ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic