Jump to content

Java Import Problem


Recommended Posts

Sun SDK 1.4.1

Importing packages aren't working.

 

The package root directory is being found (it's in the class directory) and according to Javac's verbose output the package is being loaded.

 

Trouble is although application classes compile OK they don't work properly. The programs are very simple

import uuInOut;



class Testy

 {

	 public static void main(String [] args)

    {

   int x;

 

 System.out.println("Enter number ");

 x = uuInOut.ReadInt();

 

 System.out.println("Success!" +x);

    }



 }

 

Javac Output shows :

[loading ./uupack/uuInOut.class]

 

The code from this package isn't an issue it works under Windows and with SDK 1.3 under linux any ideas? I have searched the internet religiously for 3 days trying to find out about this so far all i've got are a bundle of unhappy 1.4 users..

Link to comment
Share on other sites

Hi, thanks that was at least a partial success.

 

The .java files reliant on uuInOut do compile when it's declared as public but the program only accepts the first input then sits there doing nothing. It doesn't come up with the next user prompt and it doesn't terminate.

 

maybe the SDK is fussier under Linux than windows?

Link to comment
Share on other sites

The compiler and runtime should be fairly standard, regardless of platform - although there are some differences and it could just be a problem with the linux version. I would suggest that this is a last resort diagnosis though. If you could fnd out if it's ok to show me the source - via private message if you'd prefer - we might be able to figure this out further

Link to comment
Share on other sites

Thanks for the offer.

 

It may be the way I've done things I'm trying to find a complete beginning to end tutorial on creating and using packages.

 

I intended to ask my lecturer today if it was ok to publish his code or pass it on but I didn't see him. I'll let you know.

Link to comment
Share on other sites

Hope this isn't too basic - I'm trying to assume no knowledge

 

OK - here goes: (deep breath)

 

There are two steps to putting a class into a package - I'll use the example of a simple HelloWorld class.

 

1) Create the directory structire for a package - so, if we want org.mandrakeusers.HelloWorld then we create an org directory containing a mandrakeusers directory containing HelloWorld.class

 

Note: We often use domain names in reverse to guaruntee uniqueness of packages

 

2) We need to put a package decalration at the very top of our class so:

 

package org.mandrakeusers;



public class HelloWorld {

...

}

 

Note: package declarations MUST be at the top of the class - only comments can go above

 

HelloWorld is now in a package. You can have as many classes in one package as you like.

 

In order to use HelloWorld in aonther class, we have two options:

 

1) simply refer to it by it's full package name - e.g.:

 org.mandrakeusers.HelloWorld hello = new org.mandrakeusers.HelloWorld();

 

2) import it and then refer to it without the package structure:

 

import org.mandrakeusers.HelloWorld;



public class Whatever{

...



HelloWorld hello = new HelloWorld();



...

}

 

Note that you can use wildcards to import packages - although they only import the classes in the specified directory so: import org.mandrakeusers.* will NOT import any classes in org.mandrakusers.otherproject.

 

In order for the class to be found the org directory - NOT the class file - needs to be directly on the classpath. So if my classpath = /home/user/MyClasses then the structure will be: /home/user/MyClasses/org/mandrakeusers.HelloWorld.class

 

Final thing to mention is privacy declarations:

 

private = only visible to this class

public = visible to all classes

protected = visible to subclasses and classes in the same package

default = visible to classes in the same package

 

I realise that I probably should have put this on the web somewhererr and just linked to it - but, frankly, I couldn't be bothered :)

 

Any questions - or anything unclear, then let me know

 

Phew!

Link to comment
Share on other sites

Phunni

 

I've been pretty careful and thorough about this over the last couple of days and I still haven't got it working! I haven't finished trying but I've got to spend some time preparing for an exam:)

 

I must be doing something wrong as it appears to be working under Windows/jdk1.3 when I import an unpackaged class. It's me!

 

Thanks for bothering I appreciate your efforts. I'll be back!

Link to comment
Share on other sites

Phunni

 

I've been pretty careful and thorough about this over the last couple of days and I still haven't got it working!  I haven't finished trying but I've got to spend some time preparing for an exam:)

 

I must be doing something wrong as it appears to be working under Windows/jdk1.3 when I import an unpackaged class.  It's me!

 

Thanks for bothering I appreciate your efforts.  I'll be back!

 

Importing an upackaged class would be fine - but uuInOut is in a package - or it's declared to be

Link to comment
Share on other sites

I've done everything you've suggested but it still not working!

 

What you say is working fine under Windows.

 

Under Mandrake but it either can't find it or else compiles fine but doesn't work properly when run. (This to me is the really confusing bit)

Link to comment
Share on other sites

I'll send you what I can when I get a breathing space.

 

I've been informed that the problem might be the difference between carriage return in Windows and Linux so when I run the application it just sits there waiting for the enter key to be pressed after the first input value is entered.

 

I haven't tried yet but I'm told that pressing Ctrl and M instead should solve the problem. Check you personal messages.

 

Thanks again Will

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...