How to check answers online?

First of all sorry , if such questions are not allowed here and if such questions are allowed then please help me.I am trying to write a Java code which will take source file in C , C++ and Java and will compile and execute it and will check the solution as code chef and other websites does.Now problem is that my code is going in infinite loop and i do not know why it is happening and please if you have better way to check codes then please share your view . Here is my code :

import java.io.*;
import java.util.Scanner;

class test
{
	public static void main(String args[])
	{
		String s=null,file_name,extension;
		int pos = args[0].lastIndexOf(".");

		extension = args[0].substring(pos+1);
		file_name = args[0].substring(0,pos);

		int lang = 0; //  1 -> c,c++ , 2 -> java


		try
		{	

			Process compile = null;

			switch(extension)
			{
				case "c" 	: 	 compile = Runtime.getRuntime().exec("gcc -g "+ args[0] + " -o "+file_name+" -lm");
							lang = 1;			
							break;
				case "cpp"	:	 compile = Runtime.getRuntime().exec("g++ -g "+ args[0] + " -o "+file_name);
							lang = 1;
							break;
				case "java"	:	 compile = Runtime.getRuntime().exec("javac "+ args[0]);
							lang = 2;
			}

			BufferedReader stdError = new BufferedReader(new InputStreamReader(compile.getErrorStream()));

			if((s = stdError.readLine()) != null)
			{
				System.out.println("Compile Time Error OR Warning : ");

				System.out.println(s);
				while((s = stdError.readLine()) != null)
				{
					System.out.println(s);
				}
			}
	
			double startTime, run_time;
			Process run;

			if(lang == 1)
			{
				 startTime = System.nanoTime();

				 run = Runtime.getRuntime().exec("./"+file_name+" < "+file_name+"_input.txt > "+file_name+"_output.txt");
			
				 run_time = (System.nanoTime()-startTime)/(double)Math.pow(10,6);
			}
			else
			{
				
				startTime = System.nanoTime();

				 run = Runtime.getRuntime().exec("java "+file_name+" < "+file_name+"_input.txt > "+file_name+"_output.txt");
			
				 run_time = (System.nanoTime()-startTime)/(double)Math.pow(10,6);
			}



			System.out.println("RunTime : "+ run_time+" ms");
			
			BufferedReader out_put = new BufferedReader(new FileReader(new File(file_name+"_output.txt")));

			BufferedReader run_stdError = new BufferedReader(new InputStreamReader(run.getErrorStream()));

			
			if(( s = run_stdError.readLine()) != null)
			{
				System.out.println("Runtime Error : ");
				
				System.out.println(s);

				while((s = run_stdError.readLine()) != null )
				{
					System.out.println(s);
				}
			}
			else if((s = out_put.readLine()) != null)
			{
				String s_string = null;
				int failed = 0;
				
				File fs = new File(file_name+".txt");

				BufferedReader br  = new BufferedReader(new FileReader(fs));

				if((!s.equals(s_string = br.readLine())))
				{
					failed = 1;
				}

				while(((s = out_put.readLine()) != null) & ((s_string = br.readLine()) != null) & (failed == 0))
				{
					if(!s.equals(s_string) )
					{
						failed = 1;
						break;
					}
				}

				if((failed == 1) || s != null || s_string != null)
				{
					System.out.println("Submmision Failed : ");
					System.out.println("Either Output Is Wrong.\nOR\nYour Output Is Not According To The Given Format. ");
					System.exit(0);
					
				}
				else
				{
					System.out.println("Submission Successful.");
				}
				
			}
		}	
		catch(IOException e)
		{
			System.out.println("Some Error Has Occured : ");
			e.printStackTrace();
			System.exit(-1);
		}

	}
}

I think Runtime.getRuntime().exec() method is faulty. I would suggest using ProcessBuilder to execute external programs. Here is an example:

You provide program name and all the arguments to the constructor.

ProcessBuilder pb = new ProcessBuilder("g++","-g", source_filename,"-o",output_filename);

You need to set up a working directory.

pb.directory(new File("WORKING_DIRECTORY_HERE")); 

Then just execute the program with this call

pb.start();

Good news is that, you can redirect standard input, output and error streams of this process directly to files.
So, before calling pb.start() you should call these 3 methods as per your requirement.

pb.redirectError(new File(filename+"_stderr.txt"));
pb.redirectInput(new File(filename+"_stdin.txt"));
pb.redirectOutput(new File(filename+"_stdout.txt"));

Hope this helps. You can comment if anything is unclear from my side.

PS: Exactly one year ago, I was working on a similar project where I was executing processes from java program. :wink:

1 Like

@kapildd Thank u Kapildd , it worked.

1 Like

@sdream, plz upvote the answer, if you question solved

1 Like
//