2

I have java application which takes a file, which is created by a process running on terminal. I start the process using a small shell script. Then run the Java application and it reads the file created by the process.

Whenever this system runs created file need to be removed so that the process creates it using the same name. I have no control over the file creation process so I cant overwrite or append to the file.

My question is, on which part should the file be deleted? In the Java application or the little shell script i wrote?

I'm sorry if other aspects of the system seems poorly designed but as I said I have little control over it.

4
  • 1
    I'm a little confused by your wording "where should the file be removed?" You mean to *move it again" or delete it? Or did you want to just rename it? After this java program runs on a file, what should happen to this file?
    – Neil
    Sep 5, 2014 at 10:43
  • Sorry about the confusion, I've rephrased it. After the java program runs and terminates, files won't matter anymore. The files should not exist either at the very start of the java program or just before it terminates.
    – saccu
    Sep 5, 2014 at 11:05
  • 1
    So you want to delete a file created from a process, and you're asking if your java application should be responsible for the deletion or if you should just add a rm command within the script? Did I understand that correctly?
    – Neil
    Sep 5, 2014 at 11:12
  • Yes, that's right.
    – saccu
    Sep 5, 2014 at 11:15

2 Answers 2

2

I am big fan of stupid tools. I want my hammer to hammer and my screwdrivers to screw. If I use grep, I want to find something, but I don't want it to be able to e-mail me with the results. If I wanted that, I could pipe it to some other tool that would do that.

Likewise, you have a Java program that can do anything imaginable. Why exactly wouldn't it do anything imaginable? It is because aside from losing time implementing something that isn't required of your program, the more functionality you add to a program, the more complicated it gets. You're having to juggle the many different ways of calling the program, just to do the same thing that a tool already does, stupidly.

So in my humble opinion, you shouldn't be adding anything to the java program that can't be done in shell script. Write a stupid program and let your shell script provide the logic behind it, which makes it easy to understand how it works as well as easy to change later.

If you find yourself doing these types of things often, you might want to consider looking into Ant, which is a multiplatform scripting language based on Java.

1
  • So to avoid additional complexity, using shell script is better in this case, good answer.Thank you.
    – saccu
    Sep 5, 2014 at 11:42
1

Adding the file deletion to the Java program has the drawback of making debugging hard, assumed the Java program does not behave as expected and you want to reproduce the behaviour with the original input data.

If the Java program is called from the shell script, it will make sense to add the deletion part to the script, since in this case the script knows when the Java program ends. But you wrote

I start the process using a small shell script. Then run the Java application and it reads the file created by the process.

This sounds you are starting the Java program manually. So the script does not know when the file was processed by your Java application, or when your Java application is terminated. So adding the file deletion to the script does not make sense either.

This leaves you with the following options:

  • delete the file manually after your Java application ends
  • write a second shell script starting your Java application and deleting the file afterwards (use this script to invoke your Java application)
  • don't delete the file after the Java application ends. Instead, remove the file (if it is there from a previous execution) by the shell script before it starts the file generating process.

In most cases, I would prefer the last alternative, since it will make the reproducing of any issues much easier.

1
  • Yes at the moment, I am running the Java program manually but I also thought it would be better to start the program in the script and check for the files before program starts. So I already did the last alternative you suggested. So right now before anything ever happens the files are deleted.
    – saccu
    Sep 5, 2014 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.