2

I have a project in RAD. Package is inputFileEdit, and the java class I need is InputFileTest.java.

package inputFileEdit;

public class InputFileTest {

    public static void main(String[] args) {
        String var1 = args[0];
        String var2 = args[1].toLowerCase();

        // do stuff with args
    }
}

I want to create a new package / java program that can call or instantiate the class InputFileTest above, with arguments, multiple times in parallel. I'm basically going to be bringing back a String list, looping through that list to create parallel threads, each row on the list calling InputFileTest.

Question 1) What's the best way to call InputFileTest? I'm using RAD and I created a new Project, a package called CallerPackage, and a Caller.java inside that package? I also including a "Jar" of the whole InputFileEdit project under /lib via Java Build Path -> Libraries -> Add External Jars. I don't know how to call the class with parameters (I tried something like InputFileEdit ifeObj = new InputFileEdit("parm 1", "parm 2"); or InputFileEdit ifeObj = new InputFileEdit("parm 1 parm 2");) but neither worked so then I tried to just call the jar like Process p = Runtime.getRuntime().exec("java -jar /lib/InputFileEdit.jar parm1 parm2"); or since I want the actual Class InputFileTest, Process p = Runtime.getRuntime().exec(new String[]{"java","-cp","/lib/InputFileEdit.jar", "InputFileTest", "parm1","parm1"});:

package CallerPackage;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
//import inputFileEdit.*;

public class Caller {

     static int i = 0;

        public static void main(String args[]) throws IOException, InterruptedException {
            try {
                System.out.println("Calling jar");
                Process p = Runtime.getRuntime().exec("java -jar /lib/InputFileEdit.jar parm1 parm2");

BufferedInputStream errStrm = new BufferedInputStream(p.getErrorStream());   

                // get the error stream of the process and print it
                for (int i = 0; i < errStrm.available(); i++) {
                    System.out.println("" + errStrm.read());
                }

                System.out.println("Called jar");

                p.destroy();

            } 
            catch (Exception ex) {
                ex.printStackTrace();
            }

       }
}

but this doesn't seem to work either or print out anything helpful. Any ideas of the best way to go about this? I'm only trying to get 1 call to work for now before I loop through my list and call them in parallel. Eventually it'll be calling the jar/class looping through a string arraylist.

Paul
  • 33
  • 3

1 Answers1

0

you should be able to call the main method in InputFileTest the same way you would call any other static method: InputFileTest.main(args);

However, generally directly calling main is frowned upon. If you are able, I would advise you to take the code in InputFileTest's main, and put it into another, more descriptively named method, something like processFiles.

clearlyspam23
  • 1,624
  • 13
  • 15
  • The reason is, the inputFileEdit package has 10 java files, 1 is the InputFileTest.java with that main. If I just call the main, I get a NoClassDefFoundError when trying to access the other files. Do you think I should just add a new package within the original project with java that calls the above inputFileEdit package many times? I made a new project (CallerProject) to do this but maybe that doesn't make sense or makes it more complicated. – Paul Dec 23 '15 at 00:19
  • @Paul It's hard for me to know for sure without knowing the specifics of your situation, but I believe what you would actually want to do is probably create your new class in the same project as the class you want to call. then you should be able to just call the method as described in the answer. – clearlyspam23 Dec 23 '15 at 00:27