how to detect creation of a file...

Hi ,

I am trying to call a command line application within a java file. Its working fine by using Process.getRunTime() method. But the problem is... I need to do this a couple of times in my program. one file will be generated in the first phase,... and this will be given as input to the next command. And I am not able to find out when the file is created. If I am trying to access that file (for chekin using --"file.exists()" ), it is not created, it is getting created only after I quit from the program. But on the other hand, if I leave the program without accessing that file, it will be created even i=when the program is running. Is there any solution for this prob>

thanx in advance,

[716 byte] By [bharath@javaa] at [2007-10-1 1:32:06]
# 1

Creating an instance

File newFile = new File("path/name");

doesn't create the file, you have to write to it, and then flush/close it before it's available to another access.

If that isn't the problem, post a piece of simple code that illustrates the problem.

ChuckBinga at 2007-7-8 1:52:44 > top of Java-index,Administration Tools,Sun Connection...
# 2

Hi,

Thanx for ur help... but I am not creating file in my program. It is created by the command line program that I am using. I am including my program.

import java.io.*;

import java.lang.*;

public class test

{

public static void main(String s[])

{

try

{

Process proc = Runtime.getRuntime().exec(command);

/* "command" is the command line program I'm using... it creates req

file - opFile*/

File tempf=new File("E:/" + opFile);

while(!tempf.exists())

{

Thread.sleep(3000);

}

System.out.println("File Created");

}

catch(Exception e){}

}

};

Thanx for all your help.

bharath@javaa at 2007-7-8 1:52:44 > top of Java-index,Administration Tools,Sun Connection...
# 3
Why not wait for the process to exit? waitFor()Even if the file is available, it does not mean it is completely written. If the contents are longer than a buffer, you may find a partially written file.
BIJ001a at 2007-7-8 1:52:44 > top of Java-index,Administration Tools,Sun Connection...
# 4
Hi,Thaanx for ur concern, and of course your help. But I think the waitFor() method is abstract... is there any other way.... any way other than using Runtime.getRuntime().exec() for executing a command line program?thanx again
bharath@javaa at 2007-7-8 1:52:44 > top of Java-index,Administration Tools,Sun Connection...
# 5

> Hi,

> Thaanx for ur concern, and of course your help.

> elp. But I think the waitFor() method is abstract...

> is there any other way.... any way other than using

> Runtime.getRuntime().exec() for executing a command

> line program?

>

> thanx again

Not to my knowledge, but why have another program creat the file? Why not simply make the Java program create the file? Then you'd avoid this whole problem. Is there an obstacle keeping you from doing that?

Legend_Keepera at 2007-7-8 1:52:44 > top of Java-index,Administration Tools,Sun Connection...
# 6

Hi,

Yes, u r right. there is an obstacle. the thing is that there is a command line program which I'm using to perform a required task. So there's no other go. I hav to use it. I can simply create the file myself, but its contents are to be decided by the command line executable.

thanx for ur support... hope u'll come out with something useful for me...

bye

bharath@javaa at 2007-7-8 1:52:44 > top of Java-index,Administration Tools,Sun Connection...
# 7
Well, that was sort of my point. Why do you have to use the command line program? Why can't you use Java instead? Did you make the program you're calling fom the command line? If you didn't, how much do you know about it?
Legend_Keepera at 2007-7-8 1:52:44 > top of Java-index,Administration Tools,Sun Connection...
# 8

> But I think the waitFor() method is abstract..

This circumstance should not discourage you from its using.

Indeed, the whole Process class is abstract:

public abstract class Process

But you do not have to create an instance, you obtain it from Runtime.

You should go with waitFor() .

BIJ001a at 2007-7-8 1:52:44 > top of Java-index,Administration Tools,Sun Connection...
# 9

Perhaps it would useful to point out that it's impossible to create an instance of any abstract class. Whenever a method returns an instance of an abstract class, the instance is actually a subclass that has all methods defined.

However, if it were me, I'd still opt for writing the file in my own program if I could. o_O That would preserve the portability of your program. Is it possible for you to reproduce that program as a set of classes, or is portability not a corncern?

Legend_Keepera at 2007-7-8 1:52:44 > top of Java-index,Administration Tools,Sun Connection...
# 10
the problem here is... not abt creating the file... but I hav to use a file created by the program... it generates an XML file which I need to use in the program
bharath@javaa at 2007-7-8 1:52:44 > top of Java-index,Administration Tools,Sun Connection...
# 11

Yes, I see that, and BIJ001 has offered a solution for that.

However, why can't you write Java code to create the file instead of calling a program using Runtime? Why not write a class that generates the XML file and call its methods instead of calling on another program? Is there an obstacle that prevents you from programming such a class?

Legend_Keepera at 2007-7-8 1:52:44 > top of Java-index,Administration Tools,Sun Connection...
# 12

yes there is...... its not that simple to create that requuired XML file. there's a lot of stuff ... data to be extracted from the web, manipulate it,... filter it.....and a lot more... I realised it would take ages for me to code all those things... so I went for a program which can do that for me.

anyway.. thanx for ur concern

bharath@javaa at 2007-7-8 1:52:44 > top of Java-index,Administration Tools,Sun Connection...