package assignment;
import java.io.*;

public class AddJava{
	public static void main()
	{
		int num1,num2,sum;
		BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
		try
		{
		  System.out.println("enter first digit");
		  num1=Integer.parseInt(stdin.readLine());
		  System.out.println("enter second digit");
		  num2=Integer.parseInt(stdin.readLine());
		  sum=num1+num2;
		  System.out.println(sum);
		}
		catch(IOException e)
		{
		System.out.println("input error"+e);
	}

}
}
package assignment;

import javax.tools.*;
import java.io.*;
import java.lang.reflect.Method;
import java.net.*;
import java.util.*;

public class JavaApp 
{
	public static void main(String[] args)throws Exception
	{
        JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
		StandardJavaFileManager sjfm = jc.getStandardFileManager(null, null, null);
		File javaFile =new File("C://projectmt//workspace//mtassignment//assignment//AddJava.java");
		File[] javaFile1=new File[1];
		javaFile1[0]=javaFile;
		Iterable fileObjects = sjfm.getJavaFileObjects(javaFile1);
		String[] options = new String[]{"-d", "C://projectmt//workspace//mtassignment"};
		jc.getTask(null, null, null, Arrays.asList(options), null, fileObjects).call(); 
		try 
		{
		 sjfm.close();
		} 
		catch (IOException e)
		{
			System.out.println("error closing file manager"+e);
		}
		System.out.println("Class has been successfully compiled"); 
		URL[] urls = new URL[]{new URL("File://C://projectmt//workspace//mtassignment//")}; 
		URLClassLoader ucl = new URLClassLoader(urls);
		Class clazz = ucl.loadClass("assignment.AddJava");
		System.out.println("Class has been successfully loaded");
		Method method = clazz.getDeclaredMethod("main", null);
		Object object = clazz.newInstance();
		new threadApp();
		method.invoke(object,null);
	}
}
package assignment;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.Runnable.*;
import java.util.StringTokenizer;


public class threadApp implements Runnable{
	threadApp()
	{
		Thread t;
		t=new Thread(this,"new thread");
	    t.start();
	}
	public void run()
	{
		try
		{
		   String testNum1,testNum2;
		   int testSum;
		   BufferedReader in=new BufferedReader(new FileReader("C://projectmt//workspace//mtassignment//assignment//test.txt"));
		   String indata=in.readLine();
		   StringTokenizer tokeniser=new StringTokenizer(indata,"|"); 
		   while(tokeniser.hasMoreTokens())
	        {
	           testNum1=tokeniser.nextToken();
	 	       testNum2=tokeniser.nextToken();
	 	       testSum=Integer.parseInt(tokeniser.nextToken());
	           Thread.sleep(2000);
   	           System.out.println(testNum1);
   	           System.out.println();
   	           Thread.sleep(5000);
	           System.out.println(testNum2);
	        }
        }
		catch(InterruptedException e)
		{
			System.out.println("Child interrupted");
		}
		catch(FileNotFoundException e)
		{
			System.out.println("File not found");
		}
		catch(IOException e)
		{
			System.out.println("input error"+e);
		}
	}
}

i'm using JavaApp to compile load and execute AddJava.
test.txt is a file that contaings the various test case input against which AddJava is to be run.AddJava takes console input.Now i'm having trouble to send the contents of test.txt into the console input of AddJava.i've tried multithreading,in order to use the console output of 1 program as the console input for another,but still no luck.If anybody could help me out here,i would appreciate a lot.
thanks in advance.

It is hard to help you when you don't use code tags. You can either call the other class's main method using OtherClass.main(args go here) or you can figure out how to use Runtime.getRuntime.exec(). Those are the only two methods I know of but in my ~4 years of Java programming I haven't run into any situations where it is actually necessary to run one Java program from another. I also read your explanation a couple of times but I don't see why wanting input from a text file warrants running one java program from another. You needn't force text file input to be "rerouted" through the console (if that is what you are trying to do), simply have your program read from the text file instead.

hey,i'm doing a project which allows users to submit deir programs for a contest and than click "compile and run",so as soon as dey click the button ,the above program wil compile and run deir program against a list of test case inputs stored in a file.
so in the above example "addJava"is the program written by the user which needs to be compiled n tested.i hope this clears ur doubt.

I see. This really seems like something more suited for a scripting language (i.e. run a script that would recursively compile and run all the java files in a certain directory and store their results in different result files, possibly named based on who the original author of the .java file was - which could be accomplished through regex probably). I'm not too sure though, that is more of a guess, so maybe someone else here can either confirm that advice or give better advice of their own?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.