Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33
Like Tree1Likes

Thread: A problem with the import part

  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: A problem with the import part

    Create a folder called Testing. Add to it a folder: PathToPackage. In PathToPackage create a folder: package1.
    In package1 create a java file that is in package1 and has a static method that prints a message. Compile it.
    In the Testing folder create a java file that imports package1.* and has a main method that calls the method in the program in the package1 folder. Set classpath to PathToPackage for the compile and execute steps. Compile and execute the program in Testing
    If you don't understand my response, don't ignore it, ask a question.

  2. #22
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: A problem with the import part

    Ok but now i have this error:
    Exception in thread "main" java.lang.NoClassDefFoundError: KeyboardHookTest (wro
    ng name: de/ksquared/test/system/keyboard/KeyboardHookTest)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unkno wn Source)




    package de.ksquared.test.system.keyboard;

    import de.ksquared.system.keyboard.GlobalKeyListener;
    import de.ksquared.system.keyboard.KeyAdapter;
    import de.ksquared.system.keyboard.KeyEvent;

    import java.lang.String;

    public class KeyboardHookTest {
    public static void main(String[] args) {
    new GlobalKeyListener().addKeyListener(new KeyAdapter() {
    @Override public void keyPressed(KeyEvent event) { System.out.println(event); }
    @Override public void keyReleased(KeyEvent event) {
    System.out.println(event);
    if(event.getVirtualKeyCode()==KeyEvent.VK_ADD
    && event.isCtrlPressed())
    System.out.println("CTRL+ADD was just released (CTRL is still pressed)");
    }
    });
    while(true)
    try { Thread.sleep(100); }
    catch(InterruptedException e) { e.printStackTrace(); }
    }
    }

  3. #23
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: A problem with the import part

    Your package management (and the corresponding directory structure) is a mess: if a class C belongs to a package p, it should be stored in x/p/C.java for any directory x where x is an element of the classpath list.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: A problem with the import part

    .NoClassDefFoundError: KeyboardHookTest (wrong name: de/ksquared/test/system/keyboard/KeyboardHookTest)
    You need to specify the full classname that includes the package path when you use the java command. Also the classpath needs to point to the folder with the start of the package name.
    If you don't understand my response, don't ignore it, ask a question.

  5. #25
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: A problem with the import part

    nothing has changed

    D:\project>javac -classpath "D:\project" KeyboardHookTest.java

    D:\project>java KeyboardHookTest
    Exception in thread "main" java.lang.NoClassDefFoundError: KeyboardHookTest (wro
    ng name: de/ksquared/test/system/keyboard/KeyboardHookTest)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unkno wn Source)


    package de.ksquared.test.system.keyboard;

    import de.ksquared.system.keyboard.GlobalKeyListener;
    import de.ksquared.system.keyboard.KeyAdapter;
    import de.ksquared.system.keyboard.KeyEvent;

    public class KeyboardHookTest {
    public static void main(String[] args) {
    new GlobalKeyListener().addKeyListener(new KeyAdapter() {
    @Override public void keyPressed(KeyEvent event) { System.out.println(event); }
    @Override public void keyReleased(KeyEvent event) {
    System.out.println(event);
    if(event.getVirtualKeyCode()==KeyEvent.VK_ADD
    && event.isCtrlPressed())
    System.out.println("CTRL+ADD was just released (CTRL is still pressed)");
    }
    });
    while(true)
    try { Thread.sleep(100); }
    catch(InterruptedException e) { e.printStackTrace(); }
    }
    }

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: A problem with the import part

    NoClassDefFoundError: KeyboardHookTest (wrong name: de/ksquared/test/system/keyboard/KeyboardHookTest)
    Did you try as I suggested:
    need to specify the full classname that includes the package path when you use the java command

    The full class name is: de/ksquared/test/system/keyboard/KeyboardHookTest
    that is what is required with the java command (change the / to .)
    Not just the filename
    Last edited by Norm; 06-09-2012 at 08:21 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #27
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: A problem with the import part

    Like this : D:\project>javac -classpath "de.ksquared.test.system.keyboard.KeyboardHook Test "
    KeyboardHookTest.java

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: A problem with the import part

    I thought you were trying to use the java command? Post#25 showed there were no problems using the javac command.

    Did you ever try the simple program I suggested in post#21?
    If you don't understand my response, don't ignore it, ask a question.

  9. #29
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: A problem with the import part

    So I must use classpath in the java command?

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: A problem with the import part

    Yes, even the default to the current folder. It's value will depend on the packages being used and their locations.
    If you don't understand my response, don't ignore it, ask a question.

  11. #31
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: A problem with the import part

    Please read this part of the tutorials and don't guess anymore without knowing the details.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #32
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: A problem with the import part

    well but still i get errors

  13. #33
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: A problem with the import part

    Quote Originally Posted by gyijhbk View Post
    well but still i get errors
    For which I would recommend: solutions.

    Seriously, you have to describe your problem. That is, say what you did and what you observed. The description of what you did will include the command you used and the file (and class) structure it referred to. If what you observed included an error message, consider posting that message.

    -----

    Reading through this thread a couple of things strike me.

    First, sometimes you spell "keyboard" with the final "d", sometimes you omit it. The compiler won't be as flexible.

    Secondly, post #18. I agree with Jos: this was very good advice. Norm elaborated on it in #21, and referred you back to it in #26. And asked you a question to which you don't seem to have replied.
    Norm likes this.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. problem in java import
    By black300 in forum New To Java
    Replies: 2
    Last Post: 04-05-2011, 07:56 AM
  2. Problem import query.*
    By teaicky in forum New To Java
    Replies: 4
    Last Post: 02-05-2011, 11:09 PM
  3. Problem with import libraries
    By sarwar1234 in forum New To Java
    Replies: 3
    Last Post: 09-21-2010, 10:38 AM
  4. MultiThreading Problem part 2
    By ravjot28 in forum New To Java
    Replies: 33
    Last Post: 03-11-2010, 06:22 PM
  5. import problem
    By anki.gulsvart in forum Networking
    Replies: 9
    Last Post: 10-16-2009, 07:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •