-1

Edit: I read the similar Question, but its not working here. I have to check assignments for students. To the assignments have the same name(Ex2.java),they are in different directories. I want my test file to run all the assignments without having to go through each assignment separately.

I tried to compile and run function from another class. But there is no main there.

Lets say that I have this class without main:

public class A {

     public static int num(int x) {
           return x;
     }
}

The main class:

public class B {  
     public static void main(String[] args) throws Exception {  
         Process p = Runtime.getRuntime().exec("javac A.java");
         Process p2 = Runtime.getRuntime().exec("java A");

    }  
} 

Now I want that class B compile and run class A. Try to write something like this in class B:

A.num(x);
ShiraOzeri
  • 291
  • 4
  • 14
  • 1
    Why o why do you need to do that? – Nicholas K Dec 13 '18 at 10:46
  • 2
    Why not just import A and then something like `int y = A.num(x)` ? – Eritrean Dec 13 '18 at 10:50
  • Doesn't a simple import solve your problem? If not, why not? – Pedro Lima Dec 13 '18 at 10:51
  • 1
    Thanks for the answer and sorry I did not make myself clear enough. @NicholasK I have to check assignments for students. To the assignments have the same name(Ex2.java). I want my test file to run all the assignments without having to go through each assignment separately. – ShiraOzeri Dec 13 '18 at 10:59

1 Answers1

0

Since all files have the same name, I assume they are in different directories, right? Try making a generic test file and create a routine to create a copy of this file in each directory, then execute each of your test files, instead of the assigments.

Pedro Lima
  • 1,576
  • 12
  • 21
  • 1
    Sorry, I think that I dont know how to do this.. I read the similar questions but there solution not working to me. – ShiraOzeri Dec 13 '18 at 13:07