• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using a method from one program to work in another by importing

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please copy your code with the indentation into the CODE tags, and don't push enter at the end of every line; that post was really difficult to read. And use the code button for the tags, a lot quicker and avoids writing \ when you mean /.

What package declaration were you told to use? If you write package Lesson_1.lab1; you will get your .class files inside a folder called lab1 inside a folder called Lesson_1. If you are supposed simply to have the files inside a folder called lab1 as implied by the instruction you gave for executing the application at the end, then you should have declared the package as lab1. If you really wanted it to be in Lesson_1.lab1 then you ought to invoke it with java Lesson_1.lab1.Lab_1.
The javac instruction doesn't move the .java files; they stay wherever you put them.

To the Java Tutorials where you can read about packages and also that Lesson_1 doesn't fulfil the package name conventions.
Do a search for javac -d and you find this thread from last week where I explained about -d. Go through those sources and note the options about creating packages. I think your basic problem is that you have given the wrong name for the package in your opening statement.
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Campbell Ritchie,

Thank you for your reply.

I am sorry my post proved hard to read - I did put the code into tags- I take it the compiler output ought also be put into these tags - I will do that in future.

In copying and pasting text from notepad to the post gui, it seems text automatically becomes double spaced, in the past I used to rearrange it, but this time left it as it was - I thought double spacing might render text more legible - not less, but henceforth I will change it to single line spacing.

The text referred to firstly requests that I make a folder to hold all lessons in, called labfiles, so that is what I have done.

The text then says to place within that folder sub -folders named Lesson_1, Lesson_2 and so forth.

Then text proceeds to request that the source file Lab1_1.java be placed within the sub-folder Lesson_1, with a package statement to generate another sub-folder lab1.

In writing a package statement: Lesson_1.lab1, I had considered that I would be able to navigate easily to Lab1_1 to -d compile it, and thus create the Lesson_1 folder


and thus generated the enclosing Lesson_1 folder in that way than having created folder Lesson_1 in advance.

Alternatively I could've created the folder Lesson_1 and put it in the labfiles and input:



It seems that both ways get the same result, ie: if I create a folder Lesson_1 and input the lower version of input above - I get c:\labfiles\Lesson_1\Lab1_1.java lab1\Lab1_1.class

But, as you say Lesson_1 doesn't comply with the package naming convention, then I would stick to the

version, with the pre-made Lesson_1 folder, however this doesn't get me the importable file structure that text says program Lab1_1_2.java would require with its import statement import Lab1_1;

If I try putting the statement package lab1; as you suggested, in Lab1_1.java and -d compile it in folder

Lesson_1 in folder labfiles,
ie, again:-

then lab1 gets generated - but in the same heirarchical position as folder Lesson_1 ie : within folder labfiles - but not within folder Lesson_1, and inside thus generated lab1 folder resides Lab1_1.class, while Lab1_1.java remains inside folder Lesson_1


You said that I ought invoke with java Lesson_1.lab1.Lab_1 -meaning I ought have a file structure of Lesson_1/ lab1/Lab_1, Lab_1 being the java program - well, yes I agree that is what I am trying to get - but text says I have to get it by -d compilation.

I accept my first package statement was wrong - but the other two methods ie using package statement Lesson_1.lab1 - with folder Lesson_1 being already made, and using package statement lab1 doesn't get me the required file structure either - they land me with exactly the same file structure - that is unuseable for the subsequent requirements of the text -can I get a package statement that'll put a folder called lab1 around Lab1_1.java and Lab1_1.class - instead of it creating folder lab1 and
sticking Lab1_1.class inside it, and leaving Lab1_1.java outside of it (and thus inoperable)?


I wanted to get the file structure:

by my -d compilation -( as -d was what the text demanded)- or at least a file structure that'd permit the second program Lab1_1_2's import statement import lab1.Lab1_1 so that its Lab1_1_2's subsequent instantiation of Lab1_1, and method
call would return the string given in Lab1_1.java's myMethod(), so that in accordance with the text's request, when I put an import statement 'import lab1.Lab1_1 into program Lab1_1_2.java then Lab1_1 can be instantiated ie so that Lab1_1_2's code:



will return the same string as when Lab1_1 is run ordinarily ie:"This string returned from myMethod in the Lab1_1"

How could I rectify my package statement in Lab1_1 so's after its -d compilation, program Lab1_1_2 will run and return that string (above) when run, (I was wondering) ?
I think this question must've been meant to be a brain -teaser.

Hope you can help,

Thanks for reading.
Yours
Simon

[ September 18, 2008: Message edited by: Simon Evans ]

[ September 18, 2008: Message edited by: Simon Evans ]

[ September 18, 2008: Message edited by: Simon Evans ]

[ September 18, 2008: Message edited by: Simon Evans ]

[ September 18, 2008: Message edited by: Simon Evans ]
[edit]Correct code tags. CR[/edit]
[ September 18, 2008: Message edited by: Campbell Ritchie ]
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Campbell Ritchie,

I thought I could simplify the last post with the following summary:

If javac -d compilation to a program (say 'x') containing a package statement 'package b.c or 'package c' statement creates a c:\folder a\folder b program x.java\folder c program x.class type structure - could it not be considered that any subsequent program (say 'y') placed in folder 'a' having an 'import b.c' statement - will not compile, as the file structure generated by the -d compilation - doesn't match it.

Would that not be the situation, as things stand?

Is there a way it might be rectified ?

There isn't much I can find on -d compilation of files containing package statements, that I can find on the net, anyway.

I will have another look at my 'Horton' and 'Savitch' and 'Eckel'

I hope this post clarifies the situation, and the solution I am seeking.

Thank you for your help.

Yours

Simon.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notepad? Useless for programming. Get something decent for programming. You will find various suggestions if you search the forum, but I like Notepad2 and Notepad++ which are both available free of charge; they support syntax highlighting, bracket matching and automatic indentation, so they actually make programming easier.

No, it doesn't create that sort of folder structure at all. Either create a folder structure which matches your package structure and put the source files in the "inside" folders, in which case you don't need the -d option, OR put the source files in the "outside" folder and use the -d option.
Most IDEs use the former structure. What you seem to be suggesting is half-and-half which is neither up nor down; it didn't work for the Grand Old Duke of York and it won't work for the Java Compiler either.

You really are making things difficult for yourself. Practise with a simple package declaration without any dots in and use the three options I mentioned in that post from last week, until you get it to work. If you then find you were supposed to use a different package structure, you will be able to change it and recompile in the right folder and delete all the wrong folders. Of course that will mean changing the package declarations in two classes and recompiling which will take ages, if you are really unlucky as long as 5 minutes.
[ September 18, 2008: Message edited by: Campbell Ritchie ]
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Campbell Ritchie,

The reason I am getting stuck, is because it seems however I (or anyone might) arrange the import statement in program Lab1_1 it generates a folder layout that won't permit the import statement in program Lab1_1_2(ie: Import lab1.Lab1_1 to work, hence rendering Lab1_1_2 uncompilable.

For instance a package statement to file Lab1_1 such as: package Lesson_1.lab1;
with input:-


gets me firstly the same folder lab_files, containing the file just run Lab1_1 and the folder Lesson_1 containing the folder lab1 containing the file Lab1_1.class

If I make the package statement in Lab1_1.java from within the same with the Lesson_1 folder provided, it'll have the same result, and if I give file Lab1_1 the package statement package lab1; again from within the folder whence provided, it'll give the same result.

However this folder layout won't import to any file that holds the import statement : import lab1.Lab1_1;
such as file Lab1_1_2 has. No, instead compiler returns the following :-

I considered that the fault must lie with the package statement in file Lab1_1.java, as I am using the import statement as prescribed in Lab1_1_2.java.

The intention of the exercise was to get Lab1_1_2 to import Lab1_1, instantiate it and object method call a method within Lab1_1 that returns a String object - also within Lab1_1.

Until I can get Lab1_1 to import to Lab1_1_2, I can't proceed with this exercise - nor the SCJP course I am on.

Hoping you can tell me how I might rectify things.
Thank you for your help.
Yours
Simon (code for the two programs discussed, follows:-)




[edit]Disable smilies. CR[/edit]
[ September 21, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are using two different package structures at the same time; you have declared a package Lesson_1.lab1 and then imported a class from a package lab1 (give or take the odd capital latter).

Have you read the Java Tutorials link I posted earlier? If you have you will know that Lesson_1 is not a recommended name for a package. What exactly does the question say? Does it say to create a package lab1? In which case you ought to be declaring your package as "lab1" not "Lesson_1.lab1."
You can't import from a package called lab1_1_2 with import lab1.anything; Nobody has said anything about a folder lab1_1_2, so don't go making things more difficult for yourself than they already are.

Compile and run the following classes, which you put into any old folder:Being public classes, they require a file to themselves. Note that NameTest has NameReverser and Name as dependencies, so must be compiled last.

[Campbell@queeg applications]$ gedit NameReverser.java&
[1] 3101
[Campbell@queeg applications]$ javac -d . NameReverser.java
[Campbell@queeg applications]$ gedit Name.java&
[2] 3115
[Campbell@queeg applications]$ javac -d . Name.java
[Campbell@queeg applications]$ gedit NameTest.java&
[2] 3152
[Campbell@queeg applications]$ javac -d . NameTest.java
[Campbell@queeg applications]$ java test.NameTest campbelL ritchiE
campbelL ritchiE
Eihctir Llebpmac

Copy that code and the corresponding instructions and get it to print Evans as Snave. Copy the instructions by hand.

Then repeat what you have done for lab1.
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Campbell Ritchie,
Thank you for the programs.
I put them into a folder called labtest, and gave the following input to the console:-

[CODE]
C:\lab_test>javac Name.java

C:\lab_test>javac -d . Name.java

C:\lab_test>javac NameReverser.java

C:\lab_test>javac -d . NameReverser.java

C:\lab_test>javac NameTest.java

C:\lab_test>javac -d . NameTest.java

C:\lab_test>
[\CODE]

In accordance with the package statements: package test in NameTest, package name in NameReverse and package name in Name, the above compilation generated folders test and name, the name folder containing Name.class and NameReverse.class and the folder test containing NameTest.class.

Anyway, not having the same c:\ drive content as your computer, I wanted to know how, having compiled these programs, I might run them - specifically program NameTest, as that contains the import statement, the use of which in relation to package statements of programs it is importing - is pertinent to the exercise discussed, previously.

If I input to console's C:\lab_test>java NameTest , I get an Exception in thread "main" java.lang.NoClassDefFoundError:NameTest (wrong name: test/NameTest) return plus the associated dozen lines which come with such a return, or the same in regard to the other programs.

I will apply it to my own name - but until it runs (apart from compiling) I can't.

Hope you can help with this final bit.

Thank you for your help thus far.
Yours Simon.

[ September 21, 2008: Message edited by: Simon Evans ]
[ September 21, 2008: Message edited by: Simon Evans ]
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you read what I put in? By the way: I am running Linux, so I haven't got a "C" drive at present.

You enter

java name.NameTest snavE nomiS

and you see what happens.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Did you read what I put in? By the way: I am running Linux, so I haven't got a "C" drive at present.

You enter

java name.NameTest snavE nomiS

and you see what happens.

No you don't. Sorry. You enter

java test.NameTest snavE nomiS
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Campbell Ritchie,

I got the following returns from input to console :-

I also changed the Lab1_1's package statement to lab1 and the Lab1_1_2's import statement to Lab1_1_2
and got the desired output from the following inputs to the console:-
[CODE]

C:\lab_test>cd c:\lab_files

C:\lab_files>javac -d Lab1_1.java
javac: not a directory: Lab1_1.java
Usage: javac <options> <source files>
use -help for a list of possible options

C:\lab_files>javac Lab1_1.java

C:\lab_files>javac -d . Lab1_1.java

C:\lab_files>javac Lab1_1_2.java

C:\lab_files>java Lab1_1_2
This string returned from myMethod in the Lab1_1
This string returned from myMethod in the Lab1_1

C:\lab_files>
[\CODE]

Much thanks for your selfless help.

Yours

Simon.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
 
I found a beautiful pie. And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic