LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-16-2011, 10:39 AM   #1
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 285
Blog Entries: 1

Rep: Reputation: 37
problem in accessing java package


i am a complete newbie in java. while dealing with packages i faced the following problem.
I created a file named "CreatePackage"
Code:
package lobkush;

public class CreatePackage {
	  public CreatePackage() {
			 System.out.println("you are presently under package named \"lobkush\" ");
	  }
}
after this i created a folder named "lobkush".

I also compiled the source file and placed the .class file in the corresponding directory.

Now to test it i created another java file. This is where the problem arises.
while importing the class file if I write:

Code:
import lobkush.*;
the compiler shows an error
but if I write
Code:
import lobkush.CreatePackage;
no error is shown and the .class file generated.

Can anyone please explain to me this unnatural behaviour????
 
Old 02-17-2011, 06:47 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I think the problem is that when you use a "package", Java will expect you to have a SUBDIRECTORY for that package. For example:
Code:
cd /tmp
vi CreatePackage.java =>

package lobkush;

public class CreatePackage {
  public CreatePackage() {
    System.out.println("you are presently under package named \"lobkush\" ");
  }

  public static void main (String[] args)
  {
    CreatePackage p = new CreatePackage ();
  }
}
ls -l Create* =>
-rw-r--r--  1 paulsm users 253 2011-02-17 16:37 CreatePackage.java
Code:
javac CreatePackage.java => OK

ls -l Create* =>
-rw-r--r--  1 paulsm users 497 2011-02-17 16:39 CreatePackage.class
-rw-r--r--  1 paulsm users 253 2011-02-17 16:37 CreatePackage.java

java CreatePackage =>
Exception in thread "main" java.lang.NoClassDefFoundError: CreatePackage (wrong
name: lobkush/CreatePackage)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader
        ...
Code:
javac -d . CreatePackage.java => OK

ls -l lobkush =>
-rw-r--r--  1 paulsm users 497 2011-02-17 16:44 CreatePackage.class

java lobkush/CreatePackage =>
you are presently under package named "lobkush"
  <= This worked OK
Google for "packages" - that might make things a bit clearer.

IDE's like Eclipse and Netbeans automagically create your package subdirectories and set classpath's for you, so you don't have to worry about it so much.

And "package subdirectories" are a standard feature of .jar files.

In other words: packages are definitely "good". But some of the consequences of using packages might be a bit "surprising" at first

'Hope that helps .. PSM
 
Old 02-17-2011, 08:57 PM   #3
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 285

Original Poster
Blog Entries: 1

Rep: Reputation: 37
Well, i didn't actually mean that. You see, I know what you have just shown. Now I have a problem in accessing that package from another class in other packages.

If I use " import.lobkush.*; " keyword to include all the classes of that package, it produces an error. However when the same code is compiled against the statement " import lobkush.CreatePackage; " no error is generated.

I wish I am clear to you about my problem. Now can you please help me out???
 
Old 02-18-2011, 08:06 AM   #4
pgroover
Member
 
Registered: Sep 2005
Location: Colorado
Distribution: Ubuntu
Posts: 56

Rep: Reputation: 16
Quote:
Originally Posted by lemon09 View Post
If I use " import.lobkush.*; "
I think your problem, at least from your last post, is pretty obvious. "import.lobkush.*"...
 
Old 02-18-2011, 11:53 AM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I think "import.lobkush.*" is a typo - you actually meant "import lobkush.*". Correct?

If "import lobkush.CreatePackage" works, and "CreatePackage is a public class (as you've shown)...
... and if the directories of your client classes reside *OUTSIDE* of directory "lobkush" (as I tried to show)...
... then it should work.

If it doesn't, please post:
1. Code for your current version of "create package".
2. Code for the client class that's failing.
3. Cut/paste exact compile command and exact error.
4. Specify file names and directory locations.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem accessing java objects in netbeans jmite Programming 2 11-13-2009 06:40 AM
Accessing a java array concurrently enemorales Programming 9 11-30-2007 11:24 AM
Synaptic Package Manager shows 'java-common' and 'java-gcj-compat' as Installed. swiadek Ubuntu 3 02-12-2006 11:54 AM
Java Package problem? redshadow Programming 1 03-26-2004 07:27 AM
Java can't find package, but PATH set correctly (j2se1.4.0, java.util.regex package) Ethan Programming 5 02-06-2004 09:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration