Compile and Run a java file with the help of
the program:
A way to compile the java file with the help of the java program. The steps involved for compiling the java file from the program are described below:
For this we are taking a class Compile Hello, Inside the main method we include the following -
JavaCompiler compiler = ToolProvider.getSystemJavaComp iler();
This Creating an interface named compiler.JavaCompiler is an Interface to invoke Java programming language compilers for programs.
int result = compiler.run(null, null, null,"src/Hello.java");
This is the method of the interface JavaCompiler.This method generally invokes the tool to run with the given I/O channels and arguments.
CompileHello.java
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class CompileDemo1 {
public static void main ( String [] args ) {
JavaCompiler compiler = ToolProvider.getSystemJavaComp iler () ;
int result = compiler.run ( null, null, null, "src/Hello.java" ) ;
System.out.println ( "Compile result code = " result ) ;
}
}
Output of the program
Compile result code = 0
another way...
public class CompileDemo2 {
private static void printLines ( String name , InputStream ins ) throws Exception {
String line = null ; BufferedReader in = new BufferedReader ( new InputStreamReader ( ins ));
while (( line = in . readLine ()) != null ) {
System . out . println ( name " " line );
}
}
private static void runProcess ( String command ) throws Exception {
Process pro = Runtime . getRuntime (). exec ( command );
printLines ( command " stdout:" , pro . getInputStream ());
printLines ( command " stderr:" , pro . getErrorStream ());
pro . waitFor ();
System . out . println ( command " exitValue() " pro . exitValue ());
}
public static void main ( String [] args ) {
try {
runProcess ( "javac Main.java" );
runProcess ( "java Main" );
}
catch ( Exception e ) {
e . printStackTrace ();
}
}
}
Here is the Main.java:
public class Main {
public static void main ( String [] args ) {
System . out . println ( "ok" );
}
}
A way to compile the java file with the help of the java program. The steps involved for compiling the java file from the program are described below:
For this we are taking a class Compile Hello, Inside the main method we include the following -
JavaCompiler compiler = ToolProvider.getSystemJavaComp
This Creating an interface named compiler.JavaCompiler is an Interface to invoke Java programming language compilers for programs.
int result = compiler.run(null, null, null,"src/Hello.java");
This is the method of the interface JavaCompiler.This method generally invokes the tool to run with the given I/O channels and arguments.
CompileHello.java
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class CompileDemo1 {
public static void main ( String [] args ) {
JavaCompiler compiler = ToolProvider.getSystemJavaComp
int result = compiler.run ( null, null, null, "src/Hello.java" ) ;
System.out.println ( "Compile result code = " result ) ;
}
}
Output of the program
Compile result code = 0
another way...
public class CompileDemo2 {
private static void printLines ( String name , InputStream ins ) throws Exception {
String line = null ; BufferedReader in = new BufferedReader ( new InputStreamReader ( ins ));
while (( line = in . readLine ()) != null ) {
System . out . println ( name " " line );
}
}
private static void runProcess ( String command ) throws Exception {
Process pro = Runtime . getRuntime (). exec ( command );
printLines ( command " stdout:" , pro . getInputStream ());
printLines ( command " stderr:" , pro . getErrorStream ());
pro . waitFor ();
System . out . println ( command " exitValue() " pro . exitValue ());
}
public static void main ( String [] args ) {
try {
runProcess ( "javac Main.java" );
runProcess ( "java Main" );
}
catch ( Exception e ) {
e . printStackTrace ();
}
}
}
Here is the Main.java:
public class Main {
public static void main ( String [] args ) {
System . out . println ( "ok" );
}
}
No comments:
Post a Comment