Results 1 to 2 of 2

Thread: Setup linux and java

  1. #1
    Join Date
    Jun 2010

    Setup linux and java

    I have a simple project in eclipse, and it is structure as follows

    bin src

    In the ApplicationTest directory I did the following command
    javac -d bin -sourcepath src src/mainPkg/

    And it compiles

    Then I execute
    java /bin/mainPkg/sampleTest

    And I get this...

    Exception in thread "main" java.lang.NoClassDefFoundError: bin/mainPkg/sampleTest (wrong name: mainPkg/sampleTest)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass( :800)
    at .java:449)
    at$100(URLClassLoader. java:71)
    at$ 361)
    at$ 355)
    at Method)
    at ava:354)
    at java.lang.ClassLoader.loadClass( 25)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche
    at java.lang.ClassLoader.loadClass( 58)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Launc

    Then I specified the -cp flag, but I'm not sure which folder from the jdk directory the class path should point to.

    I did this...

    java -cp /home/user/My-Files/libraries/jdk1.7.0_45/bin/ bin/mainPkg/sampleTest

    Then I get this

    Error: Could not find or load main class bin.mainPkg.sampleTest

    It's a different error, does that mean that I specified the classpath correctly??

    Thanks a lot for your help.

  2. #2
    Join Date
    Aug 2011
    47°9′S 126°43W
    Kubuntu 14.04 Trusty Tahr

    Re: Setup linux and java

    If your class is "sampleTest" in a "mainPkg" package (which means contains "package mainPkg;") after the compile you should end up with a file bin/mainPkg/sampleTest.class.

    And to invoke it from the command line, the classpath should point to the "bin" directory and you designate the class as mainPkg.sampleTest, ie:

    java -cp bin mainPkg.sampleTest
    In other words:
    • the class path items are the directories (or jar) just above the "root" packages
    • class are designated by their full path, replacing directory separators by dots
    • the package in the class should match its position

    For instance if you have the following directory/file structure: a/b/c/d/e/Foo.class
    • if Foo has no package then the classpath should include the directory a/b/c/d/e and the class is just "Foo"
    • if Foo is declared in package "d.e" then the classpath should include the directory a/b/c and the class is d.e.Foo
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.


Posting Permissions

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