Solved

Setting execute permission to file

Posted on 2002-04-05
15
478 Views
Last Modified: 2008-03-04
I need to set read,write,execute permission to few files programmatically.

How to achieve this using java ?.

Pls help.

thanks,
0
Comment
Question by:joshidin
  • 8
  • 2
  • 2
  • +3
15 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 6920434
Because this is OS specific I don't thing it is possible to solve this problem in general. But on unix you can use:
Process p =Runtime.getRuntime().exec("chmod 700 file");
p.waitFor();

0
 
LVL 3

Expert Comment

by:msterjev
ID: 6920462
The JNI code which can do that is very long. On Windows it works with SecurityDescriptor,SID's and some LSA functions.
But, this task is worth more than 100 points!
0
 

Author Comment

by:joshidin
ID: 6920636
i'm increasing the points.....is their any java api which helps to set execute permission...Runtime.getRuntime().exec("chmod 700 file") becomes specific to unix...i need for other os also.
thanks,
0
 
LVL 2

Expert Comment

by:mzimmer74
ID: 6920709
Well, this doesn't solve the entire program, but you do realize that you can set a file to read-only don't you?  There is a method on File called setReadOnly().  Of course, I'm not finding the method to undo that so I don't know that it helps you at all.  But, it might be a place to start looking.
0
 
LVL 2

Expert Comment

by:mzimmer74
ID: 6920714
Don't know if this would help or not, but try looking at http://java.sun.com/j2se/1.3/docs/guide/security/permissions.html for information about permissions.
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6921641
This seems to be the JNI week.
The elegant solution for Windows is:

FileUtility.java


public class FileUtility
{
     public final static int FILE_ATTRIBUTE_READONLY=1;
     public final static int FILE_ATTRIBUTE_HIDDEN=2;
     public final static int FILE_ATTRIBUTE_ARCHIVE=32;
     public final static int FILE_ATTRIBUTE_NORMAL=128;
     
     static
     {
          System.loadLibrary("FileUtility");
     }
     public static native void setFileAttributes(String fileName,int fileAttributes);
}

Generate the header file FileUtility.h with:

javah FileUtility

Open Visual C++ and generate dll project with the name FileUtility.

Add the FileUtility.h to the project.
Insert new C++ file.

Paste this code here:

#include "FileUtility.h"
#include <windows.h>

JNIEXPORT void JNICALL Java_FileUtility_setFileAttributes(JNIEnv * env, jclass c, jstring f , jint a)
{
     const char *filename=env->GetStringUTFChars(f,0);
     ::SetFileAttributes(filename,a);
     env->ReleaseStringUTFChars(f,filename);
}


Put the generated dll file into the same directory as FileUtility class.

Test it:

public class Test
{
     public static void main(String[] args)
     {
          FileUtility.setFileAttributes("FileUtility.java",FileUtility.FILE_ATTRIBUTE_HIDDEN | FileUtility.FILE_ATTRIBUTE_READONLY);
     }
}

Enjoy.

P.S.
Se vrti nekoj Makedonec vo blizina???
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6921651
If you don't want JNI the the crude solution is:

1. Setting the read-only attribute:

Runtime r=Runtime.getRuntime();
r.exec("cmd.exe attrib +R "+filename);

2. Unsetting the read-only attribute:

Runtime r=Runtime.getRuntime();
r.exec("cmd.exe attrib -R "+filename);

I encourage you to use JNI.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Expert Comment

by:msterjev
ID: 6922485
Errata:
put /c  :

r.exec("cmd.exe /c attrib +R "+filename);
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6923065
Any comment....
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6923617
Did you try JNI?
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6927717
It seems you forgot this question?
0
 

Author Comment

by:joshidin
ID: 6927987
to : msterjev

No, i have not forgotten this question, some how my requirement is not met....u r dll works for windows, is their any solution for other os (Unix )....execute permission is required mainly on Unix os...

Any solution for this....

Thanks,
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6930492
As you can see you have written this:

i'm increasing the points.....is their any java api which helps to set execute permission...Runtime.getRuntime().exec("chmod
700 file") becomes specific to unix...i need for other os also.
thanks,


So, I give you windows solution also.
0
 
LVL 5

Expert Comment

by:vemul
ID: 7898904
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- To be PAQ'ed and points NOT refunded

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 7959859
Question placed in PAQ

Computer101
E-E Admin
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
json format text only 4 65
Modeling a class in java 5 34
groovy example issue 10 70
unix in java example 9 43
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now