Search code examples
javajcifs

Trying to implement jcifs and UniAddress and getting NoClassDefFoundError?


I am using JCIFS (http://jcifs.samba.org/). My code is simple and taken from the Login.java example:

import jcifs.*;
import jcifs.smb.*;

public class netp {
    public static void main( String argv[] ) throws Exception {
     System.out.println("START");

     String ip = "10.0.0.1";
     String domain = "domain";
     String user = "user";
     String pass = "pass";

    UniAddress dc = UniAddress.getByName( ip );
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication( domain + ";" + user + ":" + pass );
    SmbSession.logon( dc, auth );

     System.out.println("END");

     return;
    }
}

Compiling this works if I do this:

javac -cp jcifs-krb5-1.3.17.jar netp.java

However, if I run it like this:

java -cp jcifs-1.3.17.jar netp

I get:

Error: Could not find or load main class netp

What am I doing wrong?

I've uploaded the complete source code here:

https://www.box.com/s/po4frdmy0obqiroy9anp

Note: I am doing this all in Windows.


Solution

  • It seems that your myJavaApp class is in some package and you have omitted the package name in addition to not setting class path at all.

    My directory structure for testing:

    .
    \--- jcifs-1.3.17.jar
    \--- testapp
         \--- myJavaApp.java  
    

    I compiled it like this:

    javac -cp jcifs-1.3.17.jar testapp/myJavaApp.java
    

    which gave myJavaApp.class in testapp folder as expected. I have run it on linux like this:

    java -cp .:jcifs-1.3.17.jar testapp.myJavaApp
    

    and on windows like this:

    java -cp .;jcifs-1.3.17.jar testapp.myJavaApp
    

    It throwed

    jcifs.util.transport.TransportExceptionjava
    java.net.NoRouteToHostException: No route to host
    

    which means that the myJavaApp had run succesfully.

    If we remove the testapp directoy, e.g.

    .
    \--- jcifs-1.3.17.jar
    \--- myJavaApp.java 
    

    it compiles with:

     javac -cp jcifs-1.3.17.jar myJavaApp.java
    

    and on linux runs with:

    java -cp .:jcifs-1.3.17.jar myJavaApp
    

    for windows

    java -cp .;jcifs-1.3.17.jar myJavaApp
    

    EDIT:

    all java[c] commands were run from root(.) / testing directory

    EDIT^2:

    I have downloaded your code and placed myself in netp directory. Compiled the code like this:

    C:\netp>"C:\Program Files\Java\jdk1.6.0_25\bin\javac.exe" -cp jcifs-krb5-1.3.17.jar netp.java
    

    and succesfully run it like this:

    C:\netp>"C:\Program Files\Java\jdk1.6.0_25\bin\java.exe" -cp .;jcifs-krb5-1.3.17.jar netp
    

    it outputs:

    START
    END