0

Today I meet a problem with "java" command. I work in Intellij IDEA and think that I wrong classpath to "java" command. Please, help me.

package ru.mch;
import ru.mch.RunTask;

public class Program {
    public static void main(String[] args) {
        String taskCode = "class UserProgram{ public static void main(String[] args) { int b = 3 + 1; System.out.println(b);}}";
        String packageName = "package ru.mch; ";
        String all = packageName + taskCode ;
        RunTask runTask = new RunTask(all);
        int result = runTask.run();
    }
}

I want to get program code from String, create new java class, write code to the class and compile and run new java class.

package ru.mch;
import java.io.*;

public class RunTask {

    private String answerFromPage;
    private int programExitValue;

    public RunTask(String answerFromPage) {
        this.answerFromPage = answerFromPage;
        this.programExitValue = 0;
    }

    private static void printLines(String name, InputStream ins) throws Exception {
        String line = null;
        BufferedReader in = new BufferedReader(
                new InputStreamReader(ins));
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    }

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

    public int run(){
        //String fileName = "src\\main\\java\\ru\\mch\\UserProgram.java";
        String fileName = "src\\main\\java\\ru\\mch\\UserProgram.java";
        File f = new File(fileName);
        f.getParentFile().mkdirs();
        try {
            f.createNewFile();
        } catch (IOException e) {
            throw new IllegalArgumentException("File creating error");
        }
        try(FileWriter writer = new FileWriter(fileName, false))
        {
            writer.write(this.answerFromPage);
            writer.flush();
        }
        catch(IOException ex){
            System.out.println(ex.getMessage());
        }
        try {
            System.out.println(runProcess("javac -sourcepath src src\\main\\java\\ru\\mch\\UserProgram.java"));
            System.out.println("------------");
            this.programExitValue = runProcess("java src\\main\\java\\ru.mch.UserProgram");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this.programExitValue;
    }
}

This is IDEA log :

javac src\main\java\ru\mch\UserProgram.java exit value = 0
0
------------
Error: Could not find or load main class src\main\java\ru.mch.UserProgram
java src\main\java\ru.mch.UserProgram exit value = 1

New class was created and .class too. I try to write full classpath, try to write '\' instead of '.' in package name, but all is wrong. Sorry for my bad English.

Michael777
  • 23
  • 1
  • 5
  • Duplicate of duplicate https://stackoverflow.com/questions/7485670/error-could-not-find-or-load-main-class?rq=1 – Ivan Pronin Jul 17 '17 at 21:20
  • I try methods from those topic, but in didn't help. I think i do something wrong. That's why I ask for help. – Michael777 Jul 17 '17 at 21:48
  • If I try to run it with command line (win) I success compile it with "javac -sourcepath src\main\java src\main\ru\mch\Program.java" but I can't run it – Michael777 Jul 17 '17 at 22:10
  • [Read the documentation](http://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html) for `java` command. You specify the main class to run, you don't specify the file path. If you need to start the main class in some directory, you change the working directory to where the `.class` is located (`src\main\java` in your case) and run `java ` (`java ru.mch.UserProgram` in your case). If you can't specify the working directory, you need to specify the classpath instead using the `-cp` option. – CrazyCoder Jul 17 '17 at 22:11
  • @CrazyCoder thank you very much! I read the documentation and your answer and it it help me. The solve is java -cp src\main\java ru.mch.UserProgram Thanks! – Michael777 Jul 17 '17 at 22:23

1 Answers1

0

Use the following command:

java -cp src\main\java ru.mch.UserProgram
CrazyCoder
  • 389,263
  • 172
  • 990
  • 904