0

I tried to create a Undeletable folder using java code. I use the command "cacls (Foldername) /e /c /d %username%" in command prompt it worked fine.Then i tried to implement in my java class (Eclipse IDE). It doesn't work.

UndeletableFolder.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class UndeletableFolder {


    public static void main(String args[]){

          Runtime rt = Runtime.getRuntime(); 
                      String cmd=("cacls hidden /e /c /d %username%");
                    ProcessBuilder p = new ProcessBuilder(new String[] { "cmd.exe", "/C",
                            cmd });

                    Process pro;
                    try {
                        pro = p.start();
                        InputStream is = pro.getInputStream();
                        InputStreamReader isr = new InputStreamReader(is);
                        BufferedReader br = new BufferedReader(isr);
                        String line;
                        while ((line = br.readLine()) != null) {
                            System.out.println(line);
                        }

                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

    }


}

if any other way to do this. Thanks in advance.

Aerrow
  • 12,086
  • 10
  • 56
  • 90
  • 1
    This may be due to the %username% not being substituted when you run it from inside Java. I suggest you try to retrieve the username of the currently logged in user (using system properties) and put it into the command line instead. – Aleks G Sep 02 '11 at 11:48
  • 1
    You said "it doesn't work". That's not useful to anyone. What output did you get when you ran the code, and what did you expect to get? – Andrzej Doyle Sep 02 '11 at 11:53
  • @Aleks: I tried it doesn't work Aleks – Aerrow Sep 02 '11 at 11:54
  • @Andrezej: "It doesn't work" means- there is no output, the folder may looks like a normal folder. While i delete it was deleted – Aerrow Sep 02 '11 at 11:56
  • cacls is deprecated, try to use icacls. – Thomas Jungblut Sep 02 '11 at 12:29
  • @Thomas: I also tried with "Icacls". They also gave the same result – Aerrow Sep 02 '11 at 12:32
  • I don't understand all these questions that boil down to *"I want to force the user to..."* Let the user do what they want, but inform them that what they want might be counter-productive. They can figure it from there. – Andrew Thompson Sep 02 '11 at 13:05
  • Without details on the (lack of) results this is not a real question. Meanwhile, are you capturing the output and error streams at all? – bmargulies Sep 06 '11 at 10:24
  • @bmargulies: There is no error.After i execute this code there is no change in the folder – Aerrow Sep 06 '11 at 11:16

2 Answers2

0

Is the current working directory set to the parent directory of your "hidden" directory when you invoke the command? You can change it with ProcessBuilder.directory(java.io.File).

Barend
  • 17,296
  • 2
  • 61
  • 80
  • i give like this "pro = p.directory(java.io.File);" it gives error. – Aerrow Sep 02 '11 at 12:30
  • Of course it does. You have to pass in an instance of the class `java.io.File`, so for example `p.directory(new File("C:\\Parent\\Of\\Hidden\\Folder"));`. Also make sure you do this before you invoke `p.start()`. – Barend Sep 02 '11 at 14:04
  • sorry, it also worked same. The folder can delete as a normal folder – Aerrow Sep 02 '11 at 14:11
  • I think that's more likely to be an issue with the Windows ACL's end up on your system than with the Java code. E.g. you might be local admin on your machine or the ACL didn't get changed in the first place. – Barend Sep 02 '11 at 14:58
  • I doesn't knowledge about ACL, if any suggestion please to do this – Aerrow Sep 02 '11 at 15:07
  • Sorry, I can't help you with the Windows stuff. Try posting just the bit about how to use the cacls program on [SuperUser](http://superuser.com). – Barend Sep 02 '11 at 18:38
0

Try this code it works fine.It suits for both file and folder.

public class Undeletable {

    public static void main(String[] args) {

        Runtime runtime=Runtime.getRuntime();
        try {
            Process process= runtime.exec("cmd.exe /c  start cacls text.txt /e /d %username%");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 


}
Aerrow
  • 12,086
  • 10
  • 56
  • 90