Dear Programmers,
Perhaps you can help me avoid that banging of the head against a brick wall sensation that I am getting thus with this question, that I am obligated to answer.
Firstly I have been requested to create a program called Lab1_1 with a package declaration called lab1 and a class declaration called Lab1_1, and also a method called myMethod that will return the
String:-
"This string returned from myMethod in the Lab1_1"
and upon creating this program, save it to a folder called lab_files.
So far fairly straightforward, I thus make this little program:-
I check that this ran with the package statement commented out, and it compiled and ran okay, re:
C:\lab_files>javac Lab1_1.java
C:\lab_files>
java Lab1_1
This string returned from myMethod in the Lab1_1
C:\lab_files>
But the questioner next wants it to be javac -d compiled so that the package statement will generate the folders Lesson_1 and lab1, so (having uncommented the package line) I duly input to the jdk(1.6) :
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator>cd c:\labfilestwo
C:\labfilestwo>set PATH=C:\Program Files\Java\jdk1.6.0_05\bin;%PATH%
C:\labfilestwo>cd c:\lab_files
C:\lab_files>javac -d c:\lab_files Lab1_1.java
And the two folders appeared in the lab_files folder, folder Lesson1 being empty, and folder lab1 containing the Lab1_1 CLASS file, and the lab1_1.java file remaining back in the lab_files folder.
So far things have gone according to plan, however it is on the next few steps that I encounter difficulties: the text requests that another file called Lab1_1_2 be made and put in the same main folder ie: the lab_files folder (the main folder), with an import statment- to import the program made above ie: import lab1.Lab1_1;
and a class declaration for Lab1_1_2.
Text also requests that Lab1_1_2 ought have a main method with the following code:-
Hence I create a program that is coded thus:-
Of course due to the instancing of the imported program I can't try commenting out the import statment to see if it compiles and runs okay, but already I anticipated difficulty as text seems to presume that the javac -d compilation would've presented a folder lab1 with the Lab1_1.java file inside it - along with the Lab1_1.class file - which of course it doesn't - it has just the class file in it and the java file is still in the main lab_files folder.
Anyway the text says to compile and run the file Lab1_1_2 and it should output the same string to the console as the program Lab1_1 did (ie: whence package statement commented out, as above).
But that is not to be - as Lab1_1_2 won't even compile, if I leave the folders in the condition the javac -d compilation left them in, compiler returns:-
C:\lab_files>javac Lab1_1_2.java Lab1_1_2.java:
2: cannot access lab1.Lab1_1
bad class file: .\lab1\Lab1_1.class
class file contains wrong class: Lesson_1.lab1.Lab1_1
Please remove or make sure it appears in the correct subdirectory of the classpath.
import lab1.Lab1_1;
^
C:\lab_files>
Okay I can understand - the Lab1_1.java program isn't in the created lab1 folder, only the Lab1-1.class file is, so I try moving it to that location, and then try compiling again:-
^
C:\lab_files>javac Lab1_1_2.java Lab1_1_2.java
:2: cannot access lab1.Lab1_1
bad class file: .\lab1\Lab1_1.java
file does not contain class lab1.Lab1_1
Please remove or make sure it appears in the correct subdirectory of the classpath.
import lab1.Lab1_1;
^
C:\lab_files>
-and yet again I've got the same response.
Can anyone help out with this conudrum, as it's getting too much for the ol' brain cell.
Yours,
Desperately seeking,
Simon.
[ September 17, 2008: Message edited by: Simon Evans ]
[edit]Alter line ends. CR[/edit]
[ September 17, 2008: Message edited by: Campbell Ritchie ]