• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is that problem..  RSS feed

 
jaeha park
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i can't understand this problem

i need to help


the Problem is..

ex)

i have 2 java files in same forder.

forder name = test/
file name = test1.java / test2.java


------------------------------------------------------
"test2.java" file

package aa.data;


public class test2
{
String a ="";
String b = "";
String c = "";

test2(){}
}
------------------------------------------------------

and
------------------------------------------------------
"test1.java" file

import aa.data.*; // this line occured error

class test1
{
public static void main(String args[])
{
test2 a = new test2();

}
}
------------------------------------------------------


it's error when i compile.

of course, that source are examle and i did "javac -d . test2.java" and "javac test1.java" in test folder.

it error is " cannot access test2
bad soour file : .\test2.java !!!

it problem occurrence when 2 java files are in same folder.

if i have test2.class in class folder(test/aa/data/) and test1.java in main folder(test/)

and i do " javac test1.java " then it's no problem.


i found some answer that " 2 java files(include main file and packege file) can't be in same folder" and test1.java use " import aa.data.test2;"


i wonder that why javac compiler is looking for "~~.java" in same folder.

i want to know that distance of "import aa.data.*" and "import aa.data.test2" and how javac processed.



i need to help !! T.T




 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The folder structure on your disk must match the package structure of your classes. So if you put your classes in the package aa.data, then the file test2.java must be in a subdirectory named aa/data. For example:

C:\
|
+-- project
....|
....+-- test1.java
....+-- aa
........|
........+-- data
............|
............+-- test2.java

Then you compile it like this:

C:\project> javac aa\data\test2.java
C:\project> javac test1.java

And run it like this:

C:\project> java test1

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark your test2 constructor as public...
 
jaeha park
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for reply Jesper de Jong and Seetharaman Venkatasamy ~

I mark truth source contructor as public , but i missed this example source --; sorry.

I understand about Jesper de Jong' reply and this is helping me

but, I have some wondering , yet


In this case, I can't understand. what's wrong?

2 java source in same directory and 1 class file in sub directory, like this.

:\
|
+-- project
....|
....+-- test1.java
....+-- test2.java
....+-- aa
........|
........+-- data
............|
............+-- test2.class

when I compiled

" C:\project> javac test1.java "

but it's same error.

it's also looking for "java2.java" and it make error

is case wrong java grammar?

when i compile java source including "main", other java source including "package ~~~" can't be same directory?


I really want to know~~~~~



PS.

it's only problem when test1.java include " import aa.data.*"

no problem whth "import aa.data.test2"
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaeha park wrote:but it's same error.

Because you still don't have it organized in the way I explained.

If your class test2 is in the package aa.data, then you must have your source file test2.java in a subdirectory aa\data.

You're still not doing that - you still have test1.java and test2.java in the same directory.

This does not have anything to do with the wrong Java grammar; you have to organize your files properly.
 
jaeha park
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
jaeha park wrote:but it's same error.

Because you still don't have it organized in the way I explained.

If your class test2 is in the package aa.data, then you must have your source file test2.java in a subdirectory aa\data.

You're still not doing that - you still have test1.java and test2.java in the same directory.

This does not have anything to do with the wrong Java grammar; you have to organize your files properly.



Thanks for the reply and to care my problem. :-)

you said that it does not have anything to do with the wrong Java grammar, but i wnat to know . why that cause error?

** my question **
I hope to know why javac compiler is looking for test2.java before looking for test2.class.

just, do javac compiler works earlier to find "~.java" in same directory than find the class file in subdirectory ?
***********************************************************************************************

if test2.java is not directory then it's no error in this case

\
|
+-- project
....|
....+-- test1.java
....+-- aa
........|
........+-- data
............|
............+-- test2.class

when I complied " javac test1.java"





 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaeha park wrote:
I hope to know why javac compiler is looking for test2.java before looking for test2.class.

not really. for compiler time stamp is important. it picks latest one. and your test2 has package. so you should place the .java or .class for test2 in an appropriate package/folder.
and please take time to read this :http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!