0

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");
    }
}
Geo
  • 93,257
  • 117
  • 344
  • 520
  • Why do you cite the .java file? I hate to ask a "stupid" question, but did you compile this into a .class file? And is the directory where the path /home/geo/Geoline.class lives in the CLASSPATH? – duffymo Jul 03 '09 at 19:39
  • Yes, it's compiled. The compilation happens before the run action. Isn't the -classpath "/home/geo" loading it in the CLASSPATH? – Geo Jul 03 '09 at 19:45
  • Does the class have a package? If yes, it's the directory where the root of the package lives that has to be in the CLASSPATH. If no, add one. Classes without packages are trivial. – duffymo Jul 03 '09 at 19:54
  • The class does not belong to a package. Isn't /home/geo the good directory in this case? – Geo Jul 03 '09 at 19:55
  • just in case enable the -verbose option and see if there are any odd things – Marco Mustapic Jul 03 '09 at 20:01
  • Yes, that would be the correct directory. Does Geoline have a main method? – duffymo Jul 03 '09 at 20:24
  • yes it does. I just pasted it's contents. – Geo Jul 03 '09 at 20:26

3 Answers3

4

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
  • 196,001
  • 113
  • 385
  • 569
2

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

Nelson
  • 49,283
  • 8
  • 68
  • 81
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 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 Jul 03 '09 at 21:15
  • Tells you the pitfalls when using Runtime.exec(). Vintage 2000, but still worth knowing because Runtime hasn't changed a bit since then. – duffymo Jul 03 '09 at 21:24
1

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

[]]

user85421
  • 28,957
  • 10
  • 64
  • 87