CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2002
    Location
    Germany
    Posts
    451

    java compile, directory confusion

    I think the answer to my question must be an obvious one and shorter than even the question I am a C++ programmer struggling with the first java program for a day or so. There is some code I have to port to java (not long, only a few classes but I have to keep the structure as must as possible). First I try to have two files compiled and running but could not achieve that for a day.

    My current directory structure is like:
    c:\MyProject\proj1.java\Api\_classes
    c:\MyProject\proj1.java\Api\Api.java
    c:\MyProject\proj1.java\Api\Test\Test.java

    The files are like:
    Api.java:
    Code:
    package Api;
    
    public class Api {
    }
    Test.java:
    Code:
    package Api.Test;
    
    import Api.*;
    
    public class Test {
        public static void main(String[] args) {
            Api a = new Api();
            System.out.println("Hello, world");
        }
    }
    I actually created those with NetBeans and can compile/run. It needs to be integrated to the usual make process (now on Windows, later on linux). That should be no problem given that compilation of those will be successful (maybe .jar later, not that far yet).

    So, how far I got:
    Code:
    C:\MyProject\proj1.java>javac -d C:\MyProject\proj1.java\_classes C:\MyProject\p
    roj1.java\Api\Api.java
    OK...

    Code:
    C:\MyProject\proj1.java>javac -d C:\MyProject\proj1.java\_classes C:\MyProject\p
    roj1.java\Api\Test\Test.java
    OK...

    Code:
    C:\MyProject\proj1.java>java -cp C:\MyProject\proj1.java\_classes C:\MyProject\p
    roj1.java\_classes\Api\Test\Test
    Caused by: java.lang.ClassNotFoundException: C:\MyProject\proj1.java\_classes\Ap
    i\Test\Test
            at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    Could not find the main class: C:\MyProject\proj1.java\_classes\Api\Test\Test.
    Program will exit.
    FAIL...

    Why?
    Last edited by luftwaffe; September 20th, 2012 at 12:04 PM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: java compile, directory confusion

    Try:

    java -cp C:\MyProject\proj1.java\_classes Api\Test\Test

    BTW it's standard practice to use lower case letters for package names.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    May 2002
    Location
    Germany
    Posts
    451

    Re: java compile, directory confusion

    Thanks for the reply, at least there is some change:
    Code:
    C:\MyProject\proj1.java>java -cp C:\MyProject\proj1.java\_classes Api\Test\Test
    Exception in thread "main" java.lang.NoClassDefFoundError: Api\Test\Test (wrong
    name: Api/Test/Test)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
    1)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
            at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    Could not find the main class: Api\Test\Test.  Program will exit.
    I will most probably go for lower case as you suggested, later when this one works.

  4. #4
    Join Date
    May 2002
    Location
    Germany
    Posts
    451

    Re: java compile, directory confusion

    Solved: javac requires -cp too.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured