-1

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

Community
  • 1
  • 1
  • can you put the stacktrace of NoClassDefFoundError. For self help, this page would be useful - http://javareferencegv.blogspot.com/2013/10/debugging-javalangnoclassdeffounderror.html – Crickcoder Feb 19 '14 at 20:05
  • You know you can compile from the API, right? http://docs.oracle.com/javase/7/docs/api/javax/tools/JavaCompiler.html – Dave Newton Feb 19 '14 at 20:08
  • Why are you compiling a Java program using a Java program? – jervine10 Feb 19 '14 at 20:08
  • Out of curiosity, why are you compiling a Java application from a Java application? – Josh Feldman Feb 19 '14 at 20:08
  • so after you compile, in which folder is the class file generated? Can you see it under build\classes\pkgtry – Crickcoder Feb 19 '14 at 20:20
  • build/classes/pkgtry contains all of .class files of the project but currently after compiling Main,Main.class file is created in the Try/src/pkgtry/ – danielos_s23 Feb 19 '14 at 20:34
  • its created in the build\classes\pkgtry too,right after the compilation – danielos_s23 Feb 19 '14 at 20:36
  • when you compile using javac, it creates the class in same folder. So, the one you want to run is in the src folder and not build folder. – Crickcoder Feb 19 '14 at 21:43
  • i tried that in the first place:/ – danielos_s23 Feb 19 '14 at 22:20
  • can anybody please help?i just need to run this damn program after i compile it and @DaveNewton ive tried already the api but it's for compiling only-i need ot to execute the code himself on my console-if there's println so hell print.if there's an exception or runtime error he will show the exactly console as if i'd ran that program itslef. – danielos_s23 Feb 20 '14 at 22:41
  • You need to show the precise details, like the actual code that tries to run the class, your current working directory, etc. Right now the error message just looks like you're not setting the classpath right-classes should the last dir in the classpath, and you should be running pkgtry.Main... Just like you would from the command line. Read the error. – Dave Newton Feb 20 '14 at 22:47
  • thanks for letting me know that this is my first question on stackoverflow. I'll edit it know. – danielos_s23 Feb 21 '14 at 12:48
  • please-check the link ive attached above-it is exactly as mine but mine doesn't work-why is that?? – danielos_s23 Feb 21 '14 at 12:55
  • doesn't matter-i found a solution. – danielos_s23 Feb 23 '14 at 11:43
  • java -cp C:/Users/Daniel/Documents/NetBeansProjects/Try/src pkgtry.Main – danielos_s23 Feb 23 '14 at 12:06

1 Answers1

0

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)

user253751
  • 57,427
  • 7
  • 48
  • 90