how to run a java program stored in sub directory from another java program in the current folder [duplicate]

天涯浪子 提交于 2019-12-13 05:16:57

问题


I have big problem, please help me My situation is this, i have program RegAlloc.java, that uses several classes that is another packages P1 and P2. RegAlloc.java and these 2 packages are in a folder called Temp1. Temp is folder that contains Temp1 and Simplify.java, this folder also contains Pacjages named P! and P2 i needs to run RegAlloc from Simplify.java is there any solution? i am using the Linux platform, pls help me

try{
    String s="RegAlloc.class";
    String[] cmd = {"java", "-classpath=home/koha/pjt_new/Temp/Temp1/", s};

    Process pro2 = Runtime.getRuntime().exec(cmd); 
    BufferedReader in = new BufferedReader(new nputStreamReader(pro2.getInputStream()));

    String line = null;
    System.out.println(in.readLine());

    while ((line = in.readLine()) != null) {
        text+=line;
        text+="\n";
    }
    System.out.println(text);

} catch(Exception e) {
    System.out.println("not Okey");
}

回答1:


Set the classpath variable and then run the program

Where I have the classes like as follows

  1. COne class is in Package P1
  2. CTwo Class is in Pakcage P2
  3. Your Current folder is like F1 which contains COne.java and CTwo.java;

Compile the classes in the following manner.

javac -d . *.java

The above command will create two subdirectories in your current folder with the names P1 and P2 and the respective .class files will be pushed automatically

Set the Classpath environmental variable as :

set CLASSPATH=%CLASSPATH%;./P1;/P2;.

And Run Your Program like

java P1.COne


来源:https://stackoverflow.com/questions/19134496/how-to-run-a-java-program-stored-in-sub-directory-from-another-java-program-in-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!