Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Norm

Thread: classpath - packaging problem

  1. #1
    marquis is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default classpath - packaging problem

    Hi all, I spent all day today trying to figure this out but no luck so far, maybe a keen eye can see better than me.

    I am learning about packagin and I have a program that I am trying to run and I get the infamous error COULD NOT FIND THE MAIN CLASS

    this is the code :

    package ch4.access;

    class WithProtected {
    protected int i;
    }

    public class E06_ProtectedManipulation {
    public static void main(String args[]) {
    WithProtected wp = new WithProtected();
    wp.i = 47;
    System.out.println("wp.i = " + wp.i);
    }
    }



    and this is the error :

    d:\JavaProjects\TIJ4\ch4\access>javac E06_ProtectedManipulation.java

    d:\JavaProjects\TIJ4\ch4\access>java E06_ProtectedManipulation
    Exception in thread "main" java.lang.NoClassDefFoundError: E06_ProtectedManipu
    tion (wrong name: ch4/access/E06_ProtectedManipulation)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    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$000(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)
    Could not find the main class: E06_ProtectedManipulation. Program will exit.


    PS: the file is in here D:\JavaProjects\TIJ4\ch4\access\E06_ProtectedManip ulation.java

    CLASSPATH is set to :

    .;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip; d:\JavaProjects\TIJ4

    Running Windows 7 Professional 64 bit

    Any help would be greatly appreciated

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    wrong name: ch4/access/E06_ProtectedManipulation
    I think the runtime is complaining because you have asked to the class E06_ProtectedManipulation but there is no such class. It is suggesting that perhaps you mean the class ch4.access.E06_ProtectedManipulation because it can find that class.

    Try the following:

    Java Code:
    d:\JavaProjects\TIJ4>javac -cp . ch4\access\E06_ProtectedManipulation.java
    
    d:\JavaProjects\TIJ4>java -cp . ch4.access.E06_ProtectedManipulation
    The dash-cp-space-dot arguments are setting the classpath to be used (by both the compiler and the runtime). There is some latitude about the exact the exact commands to use, but using these two from the "top" of the desired classpath is common and straightforward. javac wants as an argument the file containing the Java source code, and that can be specified however your OS allows you to specify files at the command line. java, on the other hand, wants the name of a class which contains the main method, and that means the full name packages and all.
    Last edited by pbrockway2; 07-30-2011 at 08:41 AM.

  3. #3
    marquis is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you sir ! It works like a charm, you are the best. Now, is there a way to modify the CLASSPATH so I won't have to provide the compiler with these arguments every time? Thanks!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    is there a way to modify the CLASSPATH
    Add a . (dot) to the CLASSPATH variable.
    The . refers to the current directory.

    For the package path, you will always have to enter that.
    pbrockway2 likes this.

  5. #5
    marquis is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Ok. Thank you Norm, greatly appreciated.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I think the dot was already part of the CLASSPATH setting, so the commands I posted may well have been OK without the dash-cp-space-dot. For what it's worth, I don't use (rely on) CLASSPATH as I find one size does not fit all. Also it can solve problems before they're created to actually have to think about what you want the classpath to be.

    As your package structure gets more complicated - and as you add setting source and destination directories into the mix - typing the entire command at the command line becomes a pain. That's when things like ANT or an IDE become very useful. It's still good to know what the command line options are, however, so that you can understand the options provided by other interfaces.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Having the CLASSPATH set to a folder that you have forgotten about because your project has moved on can cause suprises.
    A lot of people think you should not use the CLASSPATH variable. Its hidden away and could be forgotten. Its bitten some students twice in the last couple of weeks. They had left off the .
    Scripts and batch files can provide the full command line you need to execute a program.

Similar Threads

  1. ANT build script, problem with packaging using rapc
    By bartosz666 in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-19-2011, 03:20 PM
  2. classpath problem, out of ideas
    By edzet in forum Advanced Java
    Replies: 16
    Last Post: 05-14-2010, 01:38 PM
  3. classpath problem
    By Nifras in forum New To Java
    Replies: 6
    Last Post: 11-28-2009, 05:49 PM
  4. classpath problem
    By shwein in forum New To Java
    Replies: 4
    Last Post: 04-01-2009, 01:32 PM
  5. Problem with CLASSPATH for STRUTS
    By bhupal4all in forum Web Frameworks
    Replies: 2
    Last Post: 11-07-2008, 06:48 AM

Posting Permissions

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