I want to save the output of a java program in a file.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I want to save the output of a java program in a file.
How is the output created and where is it written? Can you post a small, sample program that creates the output you want to write to a file?
If you don't understand my answer, don't ignore it, ask a question.
class Demo
{
public static void main(String arg[])
{
System.out.println("Yes!!!");
}
}
Above code is written in a file Demo.java.
I am using following code to run that file.
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);
pro.waitFor();
printLines(command + " stdout:", pro.getInputStream());
printLines(command + " stderr:", pro.getErrorStream());
pro.waitFor();
System.out.println(command + " exitValue() " + pro.exitValue());
}
public void run()
{
try {
runProcess("javac Demo.java");
runProcess("java Demo");
} catch (Exception e) {
e.printStackTrace();
}
}
When i am invoking run() it is showing an error:
00:32:54,073 INFO [STDOUT] java Demo stderr: java.lang.NoClassDefFoundError: Demo (wrong name: demo)
00:32:54,073 INFO [STDOUT] java Demo stderr: at java.lang.ClassLoader.defineClass1(Native Method)
00:32:54,073 INFO [STDOUT] java Demo stderr: at java.lang.ClassLoader.defineClass(Unknown Source)
00:32:54,073 INFO [STDOUT] java Demo stderr: at java.security.SecureClassLoader.defineClass(Unknow n Source)
00:32:54,073 INFO [STDOUT] java Demo stderr: at java.net.URLClassLoader.defineClass(Unknown Source)
00:32:54,073 INFO [STDOUT] java Demo stderr: at java.net.URLClassLoader.access$100(Unknown Source)
00:32:54,073 INFO [STDOUT] java Demo stderr: at java.net.URLClassLoader$1.run(Unknown Source)
00:32:54,073 INFO [STDOUT] java Demo stderr: at java.net.URLClassLoader$1.run(Unknown Source)
00:32:54,073 INFO [STDOUT] java Demo stderr: at java.security.AccessController.doPrivileged(Native Method)
00:32:54,074 INFO [STDOUT] java Demo stderr: at java.net.URLClassLoader.findClass(Unknown Source)
00:32:54,074 INFO [STDOUT] java Demo stderr: at java.lang.ClassLoader.loadClass(Unknown Source)
00:32:54,074 INFO [STDOUT] java Demo stderr: at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
00:32:54,074 INFO [STDOUT] java Demo stderr: at java.lang.ClassLoader.loadClass(Unknown Source)
00:32:54,074 INFO [STDOUT] java Demo stderr: at sun.launcher.LauncherHelper.checkAndLoadMain(Unkno wn Source)
00:32:54,074 INFO [STDOUT] java Demo stderr: Exception in thread "main"
00:32:54,074 INFO [STDOUT] java Demo exitValue() 1
Can you make a small, complete program that compiles, executes and shows the problem? Be sure to wrap the code in code tags when you post it.
If you don't understand my answer, don't ignore it, ask a question.
Is there any method in any API through which i can get the output of simple java program.
Like there is a method Diagnostic.getDiagnostic() which returns a list of Compiler errors in a program.
What kind of output are you asking about? Some output goes to a file, some goes to a socket, some goes to the console.
Are you asking about any java program or about the javac compiler?returns a list of Compiler errors in a program.
If you don't understand my answer, don't ignore it, ask a question.
i want to capture the output that goes to console.
What happens when you use a Process to execute the program? Is the program's console output written to the stream?
If you don't understand my answer, don't ignore it, ask a question.
i don't know exactly, i got this code from my friend even he doesn't know how it is working.
Do you have any idea?
Can you make a small, complete program that compiles, executes and shows the problem? Be sure to wrap the code in code tags when you post it.
That line says there was an error when "java Demo" commandline was executedjava Demo stderr: java.lang.NoClassDefFoundError: Demo (wrong name: demo)
The name of the class was wrong: demo vs Demo
If you don't understand my answer, don't ignore it, ask a question.
public List<Diagnostic<? extends JavaFileObject>> checkCompile()
{
System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_05"); //jdk path
String fileName="E:\\ProjectExp\\Demo.java"; // file path
File[] javaFiles = new File[]{new File(fileName)};
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
try
{
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asL ist(javaFiles));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null,
null, compilationUnits);
boolean success = task.call();
System.out.println(success);
if(!success)
// System.out.println(diagnostics.getDiagnostics());
return diagnostics.getDiagnostics();
fileManager.close();
}
catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return diagnostics.getDiagnostics();
}
In the above code you just need to write the path of jdk and path of file that contains the java code.
File Nameemo.java
File Content:
public class Demo
{
public static void main(String arg[])
{
System.out.println("Yes!!!");
}
}
If you want me to test the program, you need to make a small, complete program that compiles, executes and shows the problem? Be sure to wrap the code in code tags when you post it.
If you don't understand my answer, don't ignore it, ask a question.