tags:

views:

353

answers:

3

I'm executing the java binary from one of my classes, and a ClassNotFoundException gets thrown:


Results of executing:/usr/bin/java -classpath "/home/geo" Geoline
Error stream:
Exception in thread "main" java.lang.NoClassDefFoundError: Geoline
Caused by: java.lang.ClassNotFoundException: Geoline
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
Could not find the main class: Geoline. Program will exit.
Output stream:

The Geoline class is located at /home/geo/Geoline.java. The thing is, no matter where in the filesystem I'm located, if I execute the same command manually, the class is executed. Why isn't the same thing happening when the binary is executed with Runtime.getRuntime().exec?

edit: here's the output generated with the verbose flag on: pastie link edit: here's the contents of the file :


public class Geoline {
    public static void main(String[] args) {
        System.out.println("blabla");
    }
}
+3  A: 

It is quite hard to tell what's going on because you didn't post the code.

However this is my most educated guess.

You're trying to run the whole command into a java String and the java interpreter is not recognizing them as different arguments.

Try packaging them all in a String[]

So instead of

Runtime.getRuntime().execute("/usr/bin/java -classpath \"/home/geo\" Geoline");

Try with:

Runtime.getRuntime().execute( new String[]{
               "/usr/bin/java","-classpath","/home/geo","Geoline"
});

Or, you could post your actual code and we can see what's going on.

EDIT

This is my attempt. I create the RunIt.java class which is basically Runtime.exec as I suggested and Hello.java which pretty much says Hello + args[0]

Here's the ouput:

$cat RunIt.java 
import java.io.*;

public class RunIt{ 
    public static void main( String [] args ) throws IOException  {
        Process p = Runtime.getRuntime().exec(  args );
        BufferedReader reader = new BufferedReader( new InputStreamReader ( p.getInputStream() ) );
        String line = null;

        while( ( line = reader.readLine() ) != null ) {
            System.out.println( line );
        }
        reader.close();
    }
}
$cat Hello.java 
public class Hello { 
    public static void main( String [] args ) {
        System.out.println("Hola: " +  ( args.length > 0 ?  args[0] : " a nadie" ) );
    }
}
$javac RunIt.java 
$java RunIt javac Hello.java 
$java RunIt java Hello Mundo
Hola: Mundo
$
OscarRyz
This worked! Thank you Oscar! Thanks duffymo too!
Geo
+1  A: 

You might need to read this: When Runtime.exec() won't

duffymo
What's with that article? I've never feel like reading it, but it's dated 2000. Perhaps things have been fixed alredy. In Java1.5 a new class was added to the JRE: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ProcessBuilder.html I don't know if it was to fix whatever that article says. Do you have a summary?
OscarRyz
Tells you the pitfalls when using Runtime.exec(). Vintage 2000, but still worth knowing because Runtime hasn't changed a bit since then.
duffymo
+1  A: 

not sure what your system is, but Runtime.exec is not the same as a shell: try without the " around the classpath

/usr/bin/java -classpath /home/geo Geoline

(or use the exec with the array argument)
and I assume your class file is /home/geo/Geoline.class

EDIT: same happens if you try to use standard output redirection "dosomething > somefile"
Runtime.exec does not recognize this too

[]]

Carlos Heuberger