Question

i'm trying to compile & run some .java file. i used for each of them with Process.waitfor() so the compilation will complete. the compilation was successful here's my code:

    public class Try2{
private String cmd="";
  private static void printLines(String name, InputStream ins) throws Exception {
    String line = "";
    InputStreamReader isr=new InputStreamReader(ins);
    //System.out.println("INPUSTREAMREADERRRR:"+isr);
    BufferedReader in = new BufferedReader(isr);
    //System.out.println("BUFFEREDreaderrrrr:"+in );
    while ((line = in.readLine()) != null) {
        System.out.println(name + " " + line);
    }
  }

  private static void runProcess(String command) throws Exception {
    Process pro = Runtime.getRuntime().exec(command);
    printLines(command + " stdout:", pro.getInputStream());
    printLines(command + " stderr:", pro.getErrorStream());
    pro.waitFor();
    System.out.println(command + " exitValue() " + pro.exitValue());
  }

  public Try2(String cmd)
  {
      this.cmd=cmd;
  }
  public static void main(String[] args) {
      try{
      runProcess("javac C:/Users/Owner/Desktop/Daniel/Try/src/pkgtry/Main.java");
      runProcess("java Main");
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
  }
}

i took it from here: how to compile & run java program in another java program?

my Main.java contains only System.out.println("OK"); and his code works-why isnt my working?????

the output exitvalue=0,it compiles good the Main.java i checked with compilation errors like Syssdfnsdl.out.println(); and the run,right after the compilation is done: Runtime.getRuntime().exec("java -cp C:\Users\Daniel\Documents\NetBeansProjects\Try\build\classes\pkgtry Main"); i tryied something like

"java -cp . Main"

and a lot of other stuff related to this error and java command line,almost everything on stackoverflow But i can't run it,i looked all over the stackoverflow and tried to change the 'classpath' and everything.i must run it.could anybody explain me please what my codes problem is? i'm desperate. ill be very thankful.

java.lang.NoClassDefFoundError: Main (wrong name: pkgtry/Main) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:791) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482) Exception in thread "main" java -cp C:\Users\Daniel\Documents\NetBeansProjects\Try\build\classes\pkgtry Main exitValue() 1

Was it helpful?

Solution

Because your class is in a package "pkgtry", you start it with:

java -cp . pkgtry.Main

while the current directory is the directory containing the "pkgtry" directory.

You do not start it with:

java -cp . Main

while the current directory is the "pkgtry" directory. That only works if your class is not in a package (but is in a directory called "pkgtry" for whatever reason)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top