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.

Results 1 to 7 of 7

Thread: Need an sample on ProcessBuilder to delete a file.

  1. #1
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Lightbulb Need an sample on ProcessBuilder to delete a file.

    Hello forums,

    I hope you are doing great. I've been working with the ProcessBuilder lately, and I wanted to use it to send a command to the cmd prompt of windows to delete a file.

    	private static void deleteSource() throws Exception {
    		// ProcessBuilder pb = new ProcessBuilder("java", "-cp", paramContext, paramName);
    	    // p = pb.start();
    	    // processCheck();
    	    // p.waitFor();
    	}

    This is just an example to execute a .class file .. but I'd like to delete a file now. Don't know what the "(instructions)" to accomplish it.

    It has to be through the process builder, sending the file's path or name or both (i've got that covered) and not through a file object which i dont have instanciate. now thinking about it, I don't know could I instanciate a file object for an already existing file, and then just use a file method to delete it?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need an sample on ProcessBuilder to delete a file.

    Quote Originally Posted by Andrew R View Post
    This is just an example to execute a .class file .. but I'd like to delete a file now. Don't know what the "(instructions)" to accomplish it.
    If you're on windows: An A-Z Index of the Windows CMD command line | SS64.com
    If you're on linux: An A-Z Index of the Bash command line for Linux | SS64.com

    Note that this again has nothing to do with your actual program, but with command line logic. Best to boil your problem down to the smallest part possible.

    Quote Originally Posted by Andrew R View Post
    now thinking about it, I don't know could I instanciate a file object for an already existing file, and then just use a file method to delete it?
    What happened when you tried that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Andrew R (August 22nd, 2013)

  4. #3
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Need an sample on ProcessBuilder to delete a file.

    Thanks for the page. Extremely useful!

  5. #4
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Need an sample on ProcessBuilder to delete a file.

    ***Note:
    By code I mean the organized java language instructions that act as a program, and has no compilation errors.
    The collection of both .java file and .class file.

    ----

    So I have a code that creates two copies of itself with a different name. It will then send a command to shell to execute these two copies, which will continue the process. I've added some safeguards that prevents for the process to continue after a specified number of generations. Consider it as a different way of recursion.

    In addition to that, I've set up a process that after a code has finished creating it's copies, it will then proceed to delete its source code (.java). Later, during execution time of the two copies, only one of them (another safeguard) will delete the original code (.class). Forseeing the problem of having two codes trying to delete the same one.

    I've received no compilation errors from the program. I've also placed some S.o.prints to double checking the parameters or strings sent to the processbuilders. But so far the deletion is not happenning.

    Here is what I think. It might just be that I am not declaring the cmd prompt instructions properly or its a runtime issue. I've assumed that since the execution of the copies these codes have their separate individual runtimes, and could be conflicting with each other in some way. The reason I've believe this is the case its because of the order of the imput I've received.

    The logical order would be receiving info from generation 0 to start, followed by 1, 2, and so on..

    I am afraid it might also means that this is actually happening on a linear runtime all along.

    This is what I've received:

    C:\>java Earth
    OUT: OUT: Parent for generation: 2 C:\blihqwm.class
    OUT: OUT:
    OUT: Source for generation: 1 C:\\blihqwm.java
    OUT:
    OUT: Parent for generation: 1 C:\Earth.class
    OUT:
    OUT: OUT: Parent for generation: 2 C:\mwvkkff.class
    OUT: OUT:
    OUT: Source for generation: 1 C:\\mwvkkff.java
    OUT:
    Source for generation: 0 C:\\Earth.java

    	private static void deleteParent() throws Exception {
    		ProcessBuilder pb = new ProcessBuilder("DEL", parent);
    		System.out.println("Parent for generation: " + generation + " " + parent + "\n");
    	    p = pb.start();
    	    processCheck();
    	    p.waitFor();
    	}
     
    	private static void deleteSource() throws Exception {
    		ProcessBuilder pb = new ProcessBuilder("DEL", System.getProperty("user.dir") + "\\" + Thread.currentThread().getStackTrace()[1].getClassName() + ".java");
    	    System.out.println("Source for generation: " + generation + " " + System.getProperty("user.dir") + "\\" + Thread.currentThread().getStackTrace()[1].getClassName() + ".java" + "\n");
    		p = pb.start();
    	    processCheck();
    	    p.waitFor();
    	}

    Can you help me out double check if the instructions in the processbuilder are sent properly.

    Well I just noticed I haven't assigned printstacktrace in some places.. Yep I've just eddited the code.

    C:\>java Earth
    OUT: OUT: Parent for generation: 2 C:\pbsnxtf.class
    OUT: OUT:
    OUT: ERR: java.io.IOException: Cannot run program "DEL": CreateProcess error=2, The system cannot find the file specified
    OUT: ERR: at java.lang.ProcessBuilder.start(Unknown Source)
    OUT: ERR: at ccsxzde.deleteParent(ccsxzde.java:104)
    OUT: ERR: at ccsxzde.initialize(ccsxzde.java:146)
    OUT: ERR: at ccsxzde.main(ccsxzde.java:152)
    OUT: ERR: Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    OUT: ERR: at java.lang.ProcessImpl.create(Native Method)
    OUT: ERR: at java.lang.ProcessImpl.<init>(Unknown Source)
    OUT: ERR: at java.lang.ProcessImpl.start(Unknown Source)
    OUT: ERR: ... 4 more
    OUT: Source for generation: 1 C:\\pbsnxtf.java
    OUT:
    OUT: Parent for generation: 1 C:\Earth.class
    OUT:
    OUT: OUT: Parent for generation: 2 C:\uegkmks.class
    OUT: OUT:
    OUT: ERR: java.io.IOException: Cannot run program "DEL": CreateProcess error=2, The system cannot find the file specified
    OUT: ERR: at java.lang.ProcessBuilder.start(Unknown Source)
    OUT: ERR: at izwaioq.deleteParent(izwaioq.java:104)
    OUT: ERR: at izwaioq.initialize(izwaioq.java:146)
    OUT: ERR: at izwaioq.main(izwaioq.java:152)
    OUT: ERR: Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    OUT: ERR: at java.lang.ProcessImpl.create(Native Method)
    OUT: ERR: at java.lang.ProcessImpl.<init>(Unknown Source)
    OUT: ERR: at java.lang.ProcessImpl.start(Unknown Source)
    OUT: ERR: ... 4 more
    OUT: Source for generation: 1 C:\\uegkmks.java
    OUT:
    ERR: java.io.IOException: Cannot run program "DEL": CreateProcess error=2, The system cannot find the file specified
    ERR: at java.lang.ProcessBuilder.start(Unknown Source)
    ERR: at uegkmks.deleteParent(uegkmks.java:104)
    ERR: at uegkmks.initialize(uegkmks.java:146)
    ERR: at uegkmks.main(uegkmks.java:152)
    ERR: Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    ERR: at java.lang.ProcessImpl.create(Native Method)
    ERR: at java.lang.ProcessImpl.<init>(Unknown Source)
    ERR: at java.lang.ProcessImpl.start(Unknown Source)
    ERR: ... 4 more
    Source for generation: 0 C:\\Earth.java
    The problem is hell lot more descriptive than before.

  6. #5
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Need an sample on ProcessBuilder to delete a file.

    No ProcessBuilder required. It is easier to manage file objects.

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need an sample on ProcessBuilder to delete a file.

    I'm not quite sure what the status of your question is at this point. Did you figure it out? File objects aren't interchangeable with a ProcessBuilder, so I'm not sure what you mean by your last statement. And you haven't posted an SSCCE, so I'm really not sure what you're actually doing. Did you get everything figured out?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Need an sample on ProcessBuilder to delete a file.

    Using file objects, yes.

Similar Threads

  1. Replies: 0
    Last Post: January 28th, 2013, 05:29 AM
  2. Replies: 21
    Last Post: November 27th, 2012, 10:58 PM
  3. FIle Won't Delete
    By Bagzli in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 30th, 2012, 02:35 PM
  4. ProcessBuilder and I/O
    By Zo... in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 27th, 2010, 02:34 PM