Solved

Setting execute permission to file

Posted on 2002-04-05
15
484 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tomcat administrtor 12 59
ejb example issues 3 22
printf performancy 11 68
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 22
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

840 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