Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Setting execute permission to file

Posted on 2002-04-05
15
Medium Priority
?
492 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

916 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