1
1
package com .baeldung .compilerApi ;
2
2
3
+ import org .slf4j .Logger ;
4
+ import org .slf4j .LoggerFactory ;
5
+
3
6
import java .nio .file .Files ;
4
7
import java .nio .file .Path ;
5
8
import java .nio .file .Paths ;
8
11
* Demonstration of the JavaCompilerUtil class.
9
12
*/
10
13
public class JavaCompilerApiDemo {
14
+ private static final Logger logger = LoggerFactory .getLogger (JavaCompilerApiDemo .class );
11
15
12
16
public static void main (String [] args ) {
13
17
try {
14
18
// Create output directory for compiled classes
15
19
Path outputDir = Paths .get ("compiled-classes" );
16
20
17
- // Initialize the compiler utility
18
21
JavaCompilerUtils compilerUtil = new JavaCompilerUtils (outputDir );
19
- System . out . println ("Java compiler initialized with output directory: " + outputDir .toAbsolutePath ());
22
+ logger . debug ("Java compiler initialized with output directory: {}" , outputDir .toAbsolutePath ());
20
23
21
24
// Example 1: Compile from string
22
25
compileFromStringExample (compilerUtil );
@@ -25,13 +28,12 @@ public static void main(String[] args) {
25
28
compileFromFileExample (compilerUtil );
26
29
27
30
} catch (Exception e ) {
28
- System .err .println ("Error in compiler demo: " );
29
- e .printStackTrace ();
31
+ logger .error ("Compilation failed {}" , e .getMessage (), e );
30
32
}
31
33
}
32
34
33
35
private static void compileFromStringExample (JavaCompilerUtils compilerUtil ) throws Exception {
34
- System . out . println ("\n --- Example 1: Compile from String ---" );
36
+ logger . debug ("\n --- Example 1: Compile from String ---" );
35
37
36
38
// Define a simple class
37
39
String className = "HelloWorld" ;
@@ -45,20 +47,20 @@ private static void compileFromStringExample(JavaCompilerUtils compilerUtil) thr
45
47
boolean success = compilerUtil .compileFromString (className , sourceCode );
46
48
47
49
if (success ) {
48
- System . out . println ("Compilation successful!" );
49
- System . out . println ("Running the compiled class:" );
50
+ logger . debug ("Compilation successful!" );
51
+ logger . debug ("Running the compiled class:" );
50
52
51
53
// Run the compiled class
52
- System . out . println ("----- Output from HelloWorld -----" );
54
+ logger . debug ("----- Output from HelloWorld -----" );
53
55
compilerUtil .runClass (className , "arg1" , "arg2" );
54
- System . out . println ("---------------------------------" );
56
+ logger . debug ("---------------------------------" );
55
57
} else {
56
- System . out . println ("Compilation failed." );
58
+ logger . error ("Compilation failed." );
57
59
}
58
60
}
59
61
60
62
private static void compileFromFileExample (JavaCompilerUtils compilerUtil ) throws Exception {
61
- System . out . println ("\n --- Example 2: Compile from File ---" );
63
+ logger . debug ("\n --- Example 2: Compile from File ---" );
62
64
63
65
// Create a temporary Java file
64
66
Path tempFile = Paths .get ("Calculator.java" );
@@ -84,26 +86,26 @@ private static void compileFromFileExample(JavaCompilerUtils compilerUtil) throw
84
86
"}" ;
85
87
Files .write (tempFile , sourceCode .getBytes ());
86
88
87
- System . out . println ("Created temporary Java file: " + tempFile );
89
+ logger . debug ("Created temporary Java file: {}" , tempFile );
88
90
89
91
// Compile the file
90
92
boolean success = compilerUtil .compileFile (tempFile );
91
93
92
94
if (success ) {
93
- System . out . println ("Compilation successful!" );
94
- System . out . println ("Running the compiled class:" );
95
+ logger . debug ("Compilation successful!" );
96
+ logger . debug ("Running the compiled class:" );
95
97
96
98
// Run the compiled class
97
- System . out . println ("----- Output from Calculator -----" );
99
+ logger . debug ("----- Output from Calculator -----" );
98
100
compilerUtil .runClass ("Calculator" , "5" , "7" );
99
- System . out . println ("----------------------------------" );
101
+ logger . debug ("----------------------------------" );
100
102
} else {
101
103
System .out .println ("Compilation failed." );
102
104
}
103
105
104
106
// Clean up the temporary file
105
107
Files .delete (tempFile );
106
- System . out . println ("Deleted temporary file: " + tempFile );
108
+ logger . debug ("Deleted temporary file: {}" , tempFile );
107
109
}
108
110
109
111
}
0 commit comments