Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setting execute permission to file

Posted on 2002-04-05
15
Medium Priority
?
491 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

722 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