13 Replies Latest reply on Jun 18, 2009 3:50 AM by 843790

    executing a dynamically created file

    800392
      import java.io.*;
      
      public class Demo{
      
          public static void main(String[] args){
              File f = new File("abc.txt") ;
      
              try{
                      System.setOut(new PrintStream( new FileOutputStream(f) ) ) ;
              }
              catch(FileNotFoundException fnfe){
                      System.out.println(fnfe.getMessage()) ;
              }
      
              System.out.println("Hello") ;
      
              try{
                      //throwing exception,
                      //is there any method to close the f File,
                      //before we try to open the file referred by f.
                      Process p = Runtime.getRuntime().exec(f.getPath()) ;
              }
              catch(IOException io){
                      System.out.println(io.getMessage()) ;
              }
          }
      
      }
      and the content of abc.txt after executing Demo is:-

      Hello
      Cannot run program "abc.txt": CreateProcess error=32, The process cannot access the file because it is being used by another process

      how to avoid the exception.....????
      thnx in advance....
        • 1. Re: executing a dynamically created file
          843790
          try to close the stream you opened to the file (new FileOutputStream(f)).
          never leave streams open.

          something like
                  try{
                          FileOutputStream outputStream= new FileOutputStream(f) ;
                          System.setOut(new PrintStream(outputStream) ) ;
                          System.out.println("Hello") ;
                          outputStream.close();
                  }
                  catch(FileNotFoundException fnfe){
                          System.out.println(fnfe.getMessage()) ;   //BY THE WAY THIS WILL ALSO WRITE TO THE FILE IN CASE OF ERRORS. 
                  }
           
          Edited by: sushik on Jun 17, 2009 3:37 AM
          • 2. Re: executing a dynamically created file
            EJP
            Why do you have to do this via System.out? Just create a FileOutputStream, write to it, and close it.
            • 3. Re: executing a dynamically created file
              796365
              Also, there are a number of other misconceptions and errors. You are writing a line of Java code to a file that has a "txt" extension, then attempting to exec that file. That won't work: 1) The file needs to contain a valid Java class and have a "java" extension, 2) It needs to be compiled first using javac, and 4) you can't exec a class file, it has to be run using the java command.

              You really need to read and understand this article.
              http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
              • 4. Re: executing a dynamically created file
                843790
                You are writing a line of Java code to a file that has a "txt" extension,
                This appears to be a misconception or error; since when is "Hello" a line of Java code?
                • 5. Re: executing a dynamically created file
                  800392
                  ejp wrote:
                  Why do you have to do this via System.out? Just create a FileOutputStream, write to it, and close it.
                  i know that it can be done directly using FileStream, but i want to understand the meaning of the exception which is being thrown in the cases 3, 4 as explained below

                  Case 1 :*
                  import java.io.*;
                  
                  public class Demo{
                  
                       public static void main(String[] args){
                            File f = new File("abc.txt") ;
                            
                            try{
                                 //System.setOut(new PrintStream( new FileOutputStream(f) ) ) ;
                                 //System.out.println("Hello") ;
                                 Process p = Runtime.getRuntime().exec("notepad.exe " + f.getAbsolutePath()) ;
                            }
                            catch(Exception e){
                                 System.out.println(e.getMessage()) ;
                            } 
                       }
                  }
                  run perfectly without any exception and opens the abc.txt automatically as the program executes

                  Case 2 :*
                  import java.io.*;
                  
                  public class Demo{
                  
                       public static void main(String[] args){
                            File f = new File("abc.txt") ;
                            
                            try{
                                 System.setOut(new PrintStream( new FileOutputStream(f) ) ) ;
                                 System.out.println("Hello") ;
                                 Process p = Runtime.getRuntime().exec("notepad.exe " + f.getAbsolutePath()) ;
                            }
                            catch(Exception e){
                                 System.out.println(e.getMessage()) ;
                            } 
                       }
                  }
                  run perfectly without any exception and opens the abc.txt automatically as the program executes.

                  Case 3 :*
                  import java.io.*;
                  
                  public class Demo{
                  
                       public static void main(String[] args){
                            File f = new File("abc.txt") ;
                            
                            try{
                                 System.setOut(new PrintStream( new FileOutputStream(f) ) ) ;
                                 System.out.println("Hello") ;
                                 Process p = Runtime.getRuntime().exec(f.getAbsolutePath()) ;
                            }
                            catch(Exception e){
                                 System.out.println(e.getMessage()) ;
                            } 
                       }
                  }
                  don't opens the abc.txt automatically, but when i manually open it, it's contents are something like this :-

                  Hello
                  Cannot run program "D:\shanu\java": CreateProcess error=*32, The process cannot access the file because it is being used by another process*


                  Case 4 :*
                  import java.io.*;
                  
                  public class Demo{
                  
                       public static void main(String[] args){
                            File f = new File("abc.txt") ;
                            
                            try{
                                          //System.setOut(new PrintStream( new FileOutputStream(f) ) ) ;
                                 System.out.println("Hello") ;
                                 Process p = Runtime.getRuntime().exec(f.getAbsolutePath()) ;
                            }
                            catch(Exception e){
                                 System.out.println(e.getMessage()) ;
                            } 
                       }
                  }
                  don't opens the abc.txt automatically and the output on command-prompt is something like this:-

                  Hello
                  Cannot run program "D:\shanu\java": CreateProcess error=*193, %1 is not a valid Win32 application.*

                  ...................................................................................................................................................................

                  As, it is cleared from the above, that the problem is not of closing the FileOutputStream, but lies under the following statement,

                  Process p = Runtime.getRuntime().exec(f.getAbsolutePath()) ;

                  Also note the difference between the exception messages in case 3 and 4.

                  Hope, that java people here will come out with a great and satisfactory explanation.....
                  Regards rits
                  • 6. Re: executing a dynamically created file
                    843790
                    CreateProcess error=193, %1 is not a valid Win32 application.
                    So you have a file (with text inside like Hello and so on) and it is not a valid Win32 executable? How comes?

                    If you create it manually, naming some *.exe and try to run it, what do you expect to happen?
                    • 7. Re: executing a dynamically created file
                      800392
                      BIJ001 wrote:
                      CreateProcess error=193, %1 is not a valid Win32 application.
                      So you have a file (with text inside like Hello and so on) and it is not a valid Win32 executable? How comes?
                      But what about case 3 exception

                      Cannot run program "D:\shanu\java": CreateProcess error=32, The process cannot access the file because it is being used by another process
                      • 8. Re: executing a dynamically created file
                        843790
                        You have a distorted architecture, it is not the way to go what you try (this way of stdout redirection).

                        Anyway, does the name of the working directory contain a space by any chance?
                        • 9. Re: executing a dynamically created file
                          800392
                          i am trying all this stuff, to make clear the concept of Process and Runtime.exec(..........),

                          And, yes, the name of the working directory contain spaces,

                          the whole path to working directory is :
                          *D:\shanu\java [misc...]\processes and exec's*

                          But, i think that spaces are not the real probz, coz if u look at the case 1, 2 (mentioned in above post), they are working perfectly.....
                          • 10. Re: executing a dynamically created file
                            843790
                            But, i think that spaces are not the real probz, coz if u
                            http://www.catb.org/~esr/faqs/smart-questions.html#writewell

                            How To Ask Questions The Smart Way
                            Eric Steven Raymond
                            Rick Moen

                            Write in clear, grammatical, correctly-spelled language

                            We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding (often enough to bet on, anyway). Answering questions for careless and sloppy thinkers is not rewarding; we'd rather spend our time elsewhere.

                            So expressing your question clearly and well is important. If you can't be bothered to do that, we can't be bothered to pay attention. Spend the extra effort to polish your language. It doesn't have to be stiff or formal — in fact, hacker culture values informal, slangy and humorous language used with precision. But it has to be precise; there has to be some indication that you're thinking and paying attention.

                            Spell, punctuate, and capitalize correctly. Don't confuse "its" with "it's", "loose" with "lose", or "discrete" with "discreet". Don't TYPE IN ALL CAPS; this is read as shouting and considered rude. (All-smalls is only slightly less annoying, as it's difficult to read. Alan Cox can get away with it, but you can't.)

                            More generally, if you write like a semi-literate b o o b you will very likely be ignored. So don't use instant-messaging shortcuts. Spelling "you" as "u" makes you look like a semi-literate b o o b to save two entire keystrokes.
                            • 11. Re: executing a dynamically created file
                              EJP
                              i know that it can be done directly using FileStream, but i want to understand the meaning of the exception which is being thrown in the cases 3, 4 as explained below
                              The meaning of the exception is perfectly apparent. It is that you haven't closed the file, and that in turn is because you're writing it via System.out instead of FileOutputStream. So why you haven't fixed that after having been told is a mystery to me.

                              In any case 'abc.txt' is not an executable filename and 'Hello' is not an executable command. So what you are actually trying to accomplish here is also a complete mystery to me.
                              • 12. Re: executing a dynamically created file
                                796365
                                BIJ001 wrote:
                                You are writing a line of Java code to a file that has a "txt" extension,
                                This appears to be a misconception or error; since when is "Hello" a line of Java code?
                                I should have said "..attempting to write..." :)
                                • 13. Re: executing a dynamically created file
                                  843790
                                  I have to agree with most people here. it is unclear what you are trying to do but you have some general issues (coding and also pure understanding of how to do things).
                                  1. do not write to files with system out (in my original post i showed you that even errors you wrote to your file because of it, this is very bad)
                                  2. close files after finish writing to them (i wrote the code in original post, although it would be nice to also remove all those system outs as well)
                                  this is why you have: error=32, The process cannot access the file because it is being used by another process
                                  3. do not execute txt files. if you want to open a txt file with default application look at the Desktop class, not runtime.exec.
                                  this is why you have: CreateProcess error=193, %1 is not a valid Win32 application.
                                  the fact you double click a txt file on windows opens a notepad does not mean you executed the txt file, you actually executed notepad.
                                  the same functionality in java can be duplicated via Desktop.edit (or open).

                                  when you write "it is clear that the problem is that and this", you must understand your test programs are wrong and also your conclusions.
                                  try to follow the steps above and maybe read some javadocs.

                                  I'm not sure i can make it any clearer without writing the 5 lines of code for you (which won't really contribute to anything).