Post a Job  |   Ask a Question?   |   Already registered? login or register
durgapratap satyamsetti
23/05/2015 02:56 AM

EveryBody Please check this and try


Place this file in any drive String.java ------------ package java.lang; public class String{ public String toString(){ return "my own string"; } But dont compile ok And place the below file in same drive A.java ------- public class A{ public static void main(String[]args){ System.out.println("from A"); } } Now complie this A.java file (remember we should not compile String.java) Now it shows like below error error: cannot access String public static void main(String[] args) ^ bad source file : .\String.java file does not contain class String please remove or make sure it appears in the correct subdirectory of the sourcepath Why i am getting this error ? I think even compile time it also checks which string class has to taken whether from rt.jar or from my classpath.. But it even it checks in .java files also (remember we did'nt String.java file) Once try this and comment your o/p and tell your opinion I have my own opinion (may or may not right) But i want your opinion also.
Spam    2 Comments

p,sindhu
23/05/2015 04:26:16
yes,pratap i tried this,iam also getting the same problem.i think u r correct...

sreenu,kumar
23/05/2015 08:47:59
I have tried ur example error is correct, this is my openion... First of all rt.jar never contains .java files Javac compiler by default checks for .java in current directory and i don't know reason its also checks in directory java/lang(May be java.lang is package).and we can give -cp or -classpath to javac to check even in others directiries as well coming to ur example In ur String class package decleration as java.lang javac While compiling A.java it finds String and searches for String.java in current directory and it finds String.java and it tries to compile String.java while compiling it finds package java.lang and we didnt explicitely mention where to create(i.e., javac -d . String.java) Since String.java is find when we are trying to compile A.java so it checks weather .java is placed in an appropriate directry(means java\lana\String.java) so,it give and error like (Please remove or make sure it appears in the correct subdirectory of the classpath) to compile ur code try this |-A.java |-java |-lang |-String.java now javac A.java and then run java A now while compiling it compiles ur String.java and also creates String.class in directory java/lang/String.class But while Running A.class it Loads Predefined String (i.e., rt.jar String.class) why its loads predefined String we know already (i.e., Principle of Delegation) this my Openion,if iam wrong correct me........

Languages :
xml