aspose file tools*
The moose likes Beginning Java and the fly likes Thinking in Java problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Thinking in Java problem" Watch "Thinking in Java problem" New topic
Author

Thinking in Java problem

K Hans
Greenhorn

Joined: Feb 04, 2012
Posts: 7

Hello to all, this is my first time posting a question here, although I have been comming here for answers for a couple of weeks now.
I am learning Java using the Book Thinking in Java v4, I like the book and am getting alot from it so far, Granted I am only 100 pages into it.

The problem is thhe author uses his own library with a an
import static net.mindview.util.print.*; statement.
this would all be fine except that eclipse throws an error before compiling and says it cant resolve net.

I have tried many things to resolve this, I have been trying for three days now tried importing it into a lib folder in my project, tried adding it to the classpath of windows, tried converting them into a .jar file aqnd adding it to into eclipse and several other ways to make it, so far nothing helps. All of the files in his library are .java so they are source code(I elieve). Here is the listing of the print.java file from the library.

//: net/mindview/util/Print.java
// Print methods that can be used without
// qualifiers, using Java SE5 static imports:
package net.mindview.util;
import java.io.*;

public class Print {
// Print with a newline:
public static void print(Object obj) {
System.out.println(obj);
}
// Print a newline by itself:
public static void print() {
System.out.println();
}
// Print with no line break:
public static void printnb(Object obj) {
System.out.print(obj);
}
// The new Java SE5 printf() (from C):
public static PrintStream
printf(String format, Object... args) {
return System.out.printf(format, args);
}
} ///:~

thats how it is from him, I have changed nothing, I know that what this does is simple and I can just use
System.out.println("") instead but this is only the first one, he uses many more of these all through the book, and I dont want to be fighting with this all the time I am trying to learn.

any help with this would be great and very appreciated, I have found several answers to other questions here so I am hoping someone can help me.
Thanks for reading and dont be shy just tell me what you think I should try and I wil do it.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
Post your code that attempts to use his library with the exact error message you're getting, copied and pasted. When you post code, please use code tags.

Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
K Hans
Greenhorn

Joined: Feb 04, 2012
Posts: 7

ok here you go hope this is better



in the editor on the x next to the import statement the error is:
The import net cannot be resolved

in the editor when I put my mouse on the x next to the print statement it reads:
The method Print(String) is undefined for the type HelloWorld


when I try to run it in eclipse this is the error I get:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method Print(String) is undefined for the type HelloWorld
at HelloWorld.main(HelloWorld.java:8)


I used the code tag dont know if this is better or not.

Thanks K Hans

[Edit - fixed code tags - see UseCodeTags for details]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4462
    
    8

The first thing that springs to mind is that it should be import static net.mindview.util.Print.*; The capitalisation matters, and Print is a class, not a package.

(With the code tags, make sure the code is between the [code]...[/code] block)

And Welcome to The Ranch!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14339
    
  22

Also, the JAR file or directory that contains the net.mindview.util.print classes must be added to the classpath. One of the ways to do this is by using the -cp option on the command line. Example (assuming that you have a file named mindview.jar in the current directory):

javac -cp mindview.jar;. HelloWorld.java
java -cp mindview.jar;. HelloWorld


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
K Hans
Greenhorn

Joined: Feb 04, 2012
Posts: 7

OK thanks,

I tried Capitalizing Print in the import statement nothinng channged, then with that still in caps I tried caps on the print statement. then vise versa, No change at all still all the same errors.

as far as the jar goes there are no jar files at all every file has .java as the extension, none are .jar if you can tell me how to change these files into .jar and where to add them to eclipse I will try that. I did try that once before with no results but I dont know if I did it correctly.

Thanks
K Hans
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
This is a roadblock you need to get past; a learning point that everyone faces. I am not working in a Windows environment, so I can't promise your results will be exactly the same, but I suggest you try the following:

I notice that your HelloWorld.java source code specifies:



That is not correct. The name of the Thinking In Java class is "Print," the name of the package is net.mindview.util, and the names of the methods within the package are print(), printnb(), and printf().

I can't be sure which Print you're talking about having capitalized or not in your last post, so to be sure, always post updated code. Correctly capitalized source code is as shown below:



Once you confirm your code is as above and still not working, we'll move onto your directory structure.
K Hans
Greenhorn

Joined: Feb 04, 2012
Posts: 7


there you go still not working


K
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
So we move to directory structure:

BTW - I assume you're running javac and java commands from the command line, since you didn't suggest otherwise in earlier posts. If that's wrong, describe your environment and compile/run process.

In my environment, I have the directory structure:


with "HelloWorld.java" in main/ (substitute with your working source file directory on your computer) and "Print.java" placed as shown relative to main/. From a command line, I can then change to the directory "main" and type:

javac HelloWorld.java

and the file/project compiles with no errors. When done, I find HelloWorld.class in main/, and Print.class in main/net/mindview/util/. Still at a command line in the main/, I can then type

java HelloWorld

and all runs as it should.

If this directory structure doesn't work for you, or if your commands javac or java aren't recognized, please come back to let us know with any errors you've encountered.
K Hans
Greenhorn

Joined: Feb 04, 2012
Posts: 7

I am useing Eclipse SDK here is the ver num:
Eclipse SDK

Version: 3.7.1
Build id: M20110909-1335

I write my code in the eclipse editor and then click the run button and I still get the same errors.





in the editor on the x next to the import statement the error is:
The import net cannot be resolved

in the editor when I put my mouse on the x next to the print statement it reads:
The method Print(String) is undefined for the type HelloWorld

when I try to run it in eclipse this is the error I get:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method Print(String) is undefined for the type HelloWorld
at HelloWorld.main(HelloWorld.java:8)


thanks
K Hans
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
Eclipse is an editor - a fancy programmer's editor or IDE for Integrated Development Environment - that does things behind the scenes to "help" you with problems like this.

Using Eclipse's Package Explorer to view the project that contains HelloWorld.java, you should have a directory called "src", and a package subdirectory to src called "(default package)". In the (default package) subdirectory should be your HelloWorld.java source code file.

In another package subdirectory to src called "net.mindview.util", you should have the source code file called "Print.java." If you do not see this structure, then when you look at the Print.java source file you showed us in your first post, you should see an error at the package line, and when you mouse over the error, one of the options to "fix" the error should be to move the file to the appropriate location.

If you're seeing something different, fix it to be the same as I've described, and all should be okay, OR:

Alternatively:

If you've already saved the Print.java file in another package somewhere else in your Package Explorer, you can move or copy it to the location I've described, or you can change your import to match where the Print.java file really is. When you mouse over the error in your HelloWorld.java source file that says the import net.mindview.util package can't be found, Eclipse might even offer to change you're existing import statement to match where the Print.java file is located.
K Hans
Greenhorn

Joined: Feb 04, 2012
Posts: 7

Ok thanks I added the folder and the print.java to it as you said and it works now.. Thank you very much for your help.

Here is a picture of the project explorer to show how mine looks, please let me know if I did anythhing wrong.

slicpic
I tried to add the photo into the message but could not figure it out so thats a link to it.


As a final question can I just copy the source from the other .java files in the net.mindview.utill folder on my HDD into the net.mindview.util package in the eclipse project explorer So I can use the rest of them?


Again thank you for all your effort I do appreciate it, Now I can move on and keep learning.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
As a final question can I just copy the source from the other .java files in the net.mindview.utill folder on my HDD into the net.mindview.util package in the eclipse project explorer So I can use the rest of them?

I believe so. If you use Eclipse's Package Explorer with its cut/copy/paste/move menu items as desired, the recommended approach, it should keep everything up to date in your Workspace. If you do it out of Eclipse's view by adding files to the Workspace structure directly using Windows Explorer, then selecting <F5> (Refresh) in Eclipse should update the Workspace to the current status. Again, your results may vary, and it's not something I do every day so can't be sure that I remember it correctly.

Glad you finally got it working.
K Hans
Greenhorn

Joined: Feb 04, 2012
Posts: 7

Hi everyone I have been playing with this and have figured somethinng out, Just thought I would share it and maybe save someone some time.

Greg Brannon Was of great help to me and just want to say thanks again.

What I have discovered is that Greg had said to make the changes in the Package explorer in the Eclipse IDE. I discovered that I made the chages in the Project explorer.
I went back and tried it in the Package explorer and it does not work for me, I don''t know if Greg was mistaken or if it is something that changed with the new version of Eclipse or if he just misstyped.

You have to create a new source folder under the existing folder named 'src'.
In the project explorer right click the folder 'scr' then New then Source folder, name it 'net.mindview.util'
then right click the new folder 'net.mindview.util'
then click new then click package and name it 'net.mindview.util'
then right click on the package named 'net.mindview.util' and select import then select File System in the list
then click next then navigate to the folder 'net.mindview.util' on your hard drive,
there will be two boxes in the import window
the left side will have a folder named 'util' with a check box, if you check this all of the .java files from the 'net.mindview.util' will be imported into your project and everythinng will work.
The box on the right side is all the induvidual .java files and you can pick and choose what files you want to import...


I know that was alot of things to do to get it done but half the reason I am putting it here is so if I forget I can find it here too.

Hope this helps someone, So I will have given a little back to the community.


Since I first posted this I have been playing around with it more and now I am totally confused because sometimes this works and other times it doesn't not sure why but if anyone knows reply here I guess.


K Hans
 
 
subject: Thinking in Java problem