Solved

Setting execute permission to file

Posted on 2002-04-05
15
486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
maven module vs maven project 3 73
Need Help! Getting a syntax error and don't understand why 3 38
jmeter usage 4 34
learn programming 8 66
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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