• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javac problem

 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is directory:
d:/test/Demo.java
------/Demo.class
------/a/Test.java



Demo.java

Test.java


I need to complie Test file
d:/test javac -cp d:/test a/Test.java
there is complie tome error "can not find symbol:class Demo"
who could explain it for me?
thanks
[ March 17, 2006: Message edited by: Changchun Wang ]
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You have to add the package statement to Demo.java, compile the file again and import statement to class Test.java to import the Demo class, because you're trying to use Demo class and there's no way to know for the compiler where that class is then.





Command line invocation depends on in which directory you are, but you can use absolute path - putting / sing at the begining of the path, so it wouldn't matter where you are.

The command line invocation would be then:

javac -cp / /Test/a/test.java

javac -cp - that's abvious

/ - this is classpath, in order to find a class (Demo.class) that is in a package (Test) you have to include in your classpath a directory that is a superdirectory of package's root directory, in our case package's root directory is "Test" and according to your description "Test's" superdirectory is system root, so we need only "/"

/Test/a/test.java - path to your source file
 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks !Aleksander Zielinski ,you are right and your English is well
You have to add the package statement to Demo.java, compile the file again and import statement to class Test.java to import the Demo class, because you're trying to use Demo class and there's no way to know for the compiler where that class is then.

yor meaning is we can NOT import default package's members(eg: class)
in java packages
that is say:
In user-defined packages we can NOT use the class or it's members which is declared in default package
[ March 17, 2006: Message edited by: Changchun Wang ]
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

If you ask if there have to be import and package statements, the answer is no. I just thought it would be more "proffesionall" to use directory structure packages

If you remove import and package statements from source files, then for example, if we are in directory "a" the command line invocation would be:

1). Using absolute path

javac -cp /Test Test.java

2). Using relative path

javac -cp ../ Test.java
[ March 17, 2006: Message edited by: Aleksander Zielinski ]
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets take the original question into consideration. Do we have to put Demo.java in a package. Can't we do w/o that. I tried it but I was unable to compile Test.java as it could not find Demo.class.

My directory structure is

/vijay/java/Demo.java
/vijay/java/Demo.class
/vijay/java/Test.java

I am compiling Test.java from the directory /vijay/java using). I have the current directory (.) in my classpath. But still, why can't the java compiler find Demo.class.

regards,
vijay.
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You used -d option which tells the compiler where to put class files, but regardless of that you should be able to compile, because if you have all files in one directory "java" and used command line invocation:

javac -d . Test.java

that means that compiler should put all classes to the current directory and that in our case doesn't really matter.

What error did you get?
[ March 17, 2006: Message edited by: Aleksander Zielinski ]
 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Vijay Raj

you can use this

d:/vijay javac -d . java/Test.java
or d:/vijay javac -d . /vijay/java/Test.java
or d:/vijay/java javac -d . -cp ../ Test.java
[ March 17, 2006: Message edited by: Changchun Wang ]
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aleksander,

According to your previous post...
[QUOTEIf you ask if there have to be import and package statements, the answer is no. I just thought it would be more "proffesionall" to use directory structure packages
Do you mean that classes located in default package can be used only by other classes also located in default package ?

If a given class from within a package try to access another class located in default package, will I always get compile errors ?

I'm really confused about that. Could you please explain ?
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classes located in default package cannot be imported, default package can be used only from other classes in default package. That's why one should aviod using default package in professional programming.

By the way, Edisandro I sent you a private message yesterday, please read it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic