
How to set File Permission in Java? Here is a simple example of setting up file permission on any provided file.
If you are part of Operations business unit of any company then it’s very important to automate deployment workflow, analysis workflow and so on.
Sometimes your Java program exit with exception if you would try to execute some command and somehow for given file you don’t have permission to execute script file.
In our case, it’s Crunchify.sh
file.

Here is a simple Java Program which shows how to setup File Permission on any given file.
CrunchifySetFilePermission.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
package crunchify.com.java.tutorials; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.attribute.PosixFilePermission; import java.util.HashSet; import java.util.Set; import java.io.File; /** * @author Crunchify.com * In Java how to Set File Permission on a File using PosixFilePermission? Understanding chmod command. */ public class CrunchifySetFilePermission { public static void main(String[] args) { // File class: An abstract representation of file and directory pathnames. // User interfaces and operating systems use system-dependent pathname strings to name files and directories. // This class presents an abstract, system-independent view of hierarchical pathnames. File file = new File("/Users/app/Download/crunchify.txt"); /* -------------------------- Set Permission: 455 ----------------------- */ // A convenience method to set the owner's execute permission for this abstract pathname. // On some platforms it may be possible to start the Java virtual machine with special privileges that allow it to execute files that are not marked executable. file.setExecutable(false); // public boolean setExecutable(boolean executable) // A convenience method to set the owner's read permission for this abstract pathname. // On some platforms it may be possible to start the Java virtual machine with special privileges that allow it to read files that are marked as unreadable. file.setReadable(false); // A convenience method to set the owner's write permission for this abstract pathname. // On some platforms it may be possible to start the Java virtual machine with special privileges that allow it to modify files that disallow write operations. file.setWritable(true); /* -------------------------- Set Permission: 777 ----------------------- */ // public boolean setExecutable(boolean executable, boolean ownerOnly) file.setExecutable(true, false); file.setReadable(true, false); file.setWritable(true, false); // using PosixFilePermission to set file permissions 777. PosixFilePermission - defines the bits for use with the permissions attribute. // The PosixFilePermissions class defines methods for manipulating set of permissions // HashSet: Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75). Set<PosixFilePermission> crunchifyPermissions = new HashSet<PosixFilePermission>(); // add() Adds the specified element to this set if it is not already present (optional operation). // More formally, adds the specified element e to this set if the set contains no element e2 such that Objects.equals(e, e2). // If this set already contains the element, the call leaves the set unchanged and returns false. // In combination with the restriction on constructors, this ensures that sets never contain duplicate elements. /* -------------------------- OWNER Permissions ----------------------- */ // Read permission, owner. crunchifyPermissions.add(PosixFilePermission.OWNER_READ); // Write permission, owner. crunchifyPermissions.add(PosixFilePermission.OWNER_WRITE); // Execute/search permission, owner. crunchifyPermissions.add(PosixFilePermission.OWNER_EXECUTE); /* -------------------------- GROUP Permissions ----------------------- */ // PosixFilePermission: Defines the bits for use with the permissions attribute. // The PosixFilePermissions class defines methods for manipulating set of permissions. // Read permission, group. crunchifyPermissions.add(PosixFilePermission.GROUP_READ); // Write permission, group. crunchifyPermissions.add(PosixFilePermission.GROUP_WRITE); // Execute/search permission, group. crunchifyPermissions.add(PosixFilePermission.GROUP_EXECUTE); /* -------------------------- OTHERS Permissions ----------------------- */ // Read permission, others. crunchifyPermissions.add(PosixFilePermission.OTHERS_READ); // Write permission, others. crunchifyPermissions.add(PosixFilePermission.OTHERS_WRITE); // Execute/search permission, others. crunchifyPermissions.add(PosixFilePermission.OTHERS_EXECUTE); try { // Sets a file's POSIX permissions. // The path parameter is associated with a FileSystem that supports the PosixFileAttributeView. This attribute view provides access to file // attributes commonly associated with files on file systems used by operating systems that implement the Portable Operating System Interface (POSIX) family of standards. Files.setPosixFilePermissions(Paths.get("/Users/app/Download/crunchify.sh"), crunchifyPermissions); // Paths: This class consists exclusively of static methods that return a Path by converting a path string or URI. System.out.println("File permission set successfully..."); } catch (IOException e) { // Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. e.printStackTrace(); } } } |
Just run above Java Program and you should see permission of file Crunchify.sh as below.
1 |
-rwxrwxrwx 1 crunchify crunchify 65 Jun 23 14:42 crunchify.sh |
If you remove or comment out below 3 files then it will change permission as below:
1 2 3 |
// crunchifyPermissions.add(PosixFilePermission.OTHERS_READ); // crunchifyPermissions.add(PosixFilePermission.OTHERS_WRITE); // crunchifyPermissions.add(PosixFilePermission.OTHERS_EXECUTE); |
1 |
-rwxrwx--- 1 crunchify crunchify 65 Jun 23 14:42 crunchify.sh |
Let’s understand Linux File Permissions:

Here is a list of all permissions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 |
100 ---x------ 101 ---x-----x 102 ---x----w- 103 ---x----wx 104 ---x---r-- 105 ---x---r-x 106 ---x---rw- 107 ---x---rwx 110 ---x--x--- 111 ---x--x--x 112 ---x--x-w- 113 ---x--x-wx 114 ---x--xr-- 115 ---x--xr-x 116 ---x--xrw- 117 ---x--xrwx 120 ---x-w---- 121 ---x-w---x 122 ---x-w--w- 123 ---x-w--wx 124 ---x-w-r-- 125 ---x-w-r-x 126 ---x-w-rw- 127 ---x-w-rwx 130 ---x-wx--- 131 ---x-wx--x 132 ---x-wx-w- 133 ---x-wx-wx 134 ---x-wxr-- 135 ---x-wxr-x 136 ---x-wxrw- 137 ---x-wxrwx 140 ---xr----- 141 ---xr----x 142 ---xr---w- 143 ---xr---wx 144 ---xr--r-- 145 ---xr--r-x 146 ---xr--rw- 147 ---xr--rwx 150 ---xr-x--- 151 ---xr-x--x 152 ---xr-x-w- 153 ---xr-x-wx 154 ---xr-xr-- 155 ---xr-xr-x 156 ---xr-xrw- 157 ---xr-xrwx 160 ---xrw---- 161 ---xrw---x 162 ---xrw--w- 163 ---xrw--wx 164 ---xrw-r-- 165 ---xrw-r-x 166 ---xrw-rw- 167 ---xrw-rwx 170 ---xrwx--- 171 ---xrwx--x 172 ---xrwx-w- 173 ---xrwx-wx 174 ---xrwxr-- 175 ---xrwxr-x 176 ---xrwxrw- 177 ---xrwxrwx 226 --w--w-rw- 227 --w--w-rwx 241 --w-r----x 242 --w-r---w- 243 --w-r---wx 244 --w-r--r-- 245 --w-r--r-x 246 --w-r--rw- 247 --w-r--rwx 250 --w-r-x--- 251 --w-r-x--x 252 --w-r-x-w- 253 --w-r-x-wx 254 --w-r-xr-- 255 --w-r-xr-x 256 --w-r-xrw- 257 --w-r-xrwx 260 --w-rw---- 261 --w-rw---x 262 --w-rw--w- 263 --w-rw--wx 264 --w-rw-r-- 265 --w-rw-r-x 266 --w-rw-rw- 267 --w-rw-rwx 270 --w-rwx--- 271 --w-rwx--x 272 --w-rwx-w- 273 --w-rwx-wx 274 --w-rwxr-- 275 --w-rwxr-x 276 --w-rwxrw- 277 --w-rwxrwx 300 --wx------ 301 --wx-----x 302 --wx----w- 303 --wx----wx 304 --wx---r-- 305 --wx---r-x 306 --wx---rw- 307 --wx---rwx 310 --wx--x--- 311 --wx--x--x 312 --wx--x-w- 313 --wx--x-wx 314 --wx--xr-- 315 --wx--xr-x 316 --wx--xrw- 317 --wx--xrwx 320 --wx-w---- 321 --wx-w---x 322 --wx-w--w- 323 --wx-w--wx 324 --wx-w-r-- 325 --wx-w-r-x 326 --wx-w-rw- 327 --wx-w-rwx 330 --wx-wx--- 331 --wx-wx--x 332 --wx-wx-w- 333 --wx-wx-wx 334 --wx-wxr-- 335 --wx-wxr-x 336 --wx-wxrw- 337 --wx-wxrwx 340 --wxr----- 341 --wxr----x 342 --wxr---w- 343 --wxr---wx 344 --wxr--r-- 345 --wxr--r-x 346 --wxr--rw- 347 --wxr--rwx 350 --wxr-x--- 351 --wxr-x--x 352 --wxr-x-w- 353 --wxr-x-wx 354 --wxr-xr-- 355 --wxr-xr-x 356 --wxr-xrw- 357 --wxr-xrwx 360 --wxrw---- 361 --wxrw---x 362 --wxrw--w- 363 --wxrw--wx 364 --wxrw-r-- 365 --wxrw-r-x 366 --wxrw-rw- 367 --wxrw-rwx 370 --wxrwx--- 371 --wxrwx--x 372 --wxrwx-w- 373 --wxrwx-wx 374 --wxrwxr-- 375 --wxrwxr-x 376 --wxrwxrw- 377 --wxrwxrwx 400 -r-------- 401 -r-------x 402 -r------w- 403 -r------wx 404 -r-----r-- 405 -r-----r-x 406 -r-----rw- 407 -r-----rwx 410 -r----x--- 411 -r----x--x 412 -r----x-w- 413 -r----x-wx 414 -r----xr-- 415 -r----xr-x 416 -r----xrw- 417 -r----xrwx 420 -r---w---- 421 -r---w---x 422 -r---w--w- 423 -r---w--wx 424 -r---w-r-- 425 -r---w-r-x 426 -r---w-rw- 427 -r---w-rwx 430 -r---wx--- 431 -r---wx--x 432 -r---wx-w- 433 -r---wx-wx 434 -r---wxr-- 435 -r---wxr-x 436 -r---wxrw- 437 -r---wxrwx 440 -r--r----- 441 -r--r----x 442 -r--r---w- 443 -r--r---wx 444 -r--r--r-- 445 -r--r--r-x 446 -r--r--rw- 447 -r--r--rwx 450 -r--r-x--- 451 -r--r-x--x 452 -r--r-x-w- 453 -r--r-x-wx 454 -r--r-xr-- 455 -r--r-xr-x 456 -r--r-xrw- 457 -r--r-xrwx 460 -r--rw---- 461 -r--rw---x 462 -r--rw--w- 463 -r--rw--wx 464 -r--rw-r-- 465 -r--rw-r-x 466 -r--rw-rw- 467 -r--rw-rwx 470 -r--rwx--- 471 -r--rwx--x 472 -r--rwx-w- 473 -r--rwx-wx 474 -r--rwxr-- 475 -r--rwxr-x 476 -r--rwxrw- 477 -r--rwxrwx 500 -r-x------ 501 -r-x-----x 502 -r-x----w- 503 -r-x----wx 504 -r-x---r-- 505 -r-x---r-x 506 -r-x---rw- 507 -r-x---rwx 510 -r-x--x--- 511 -r-x--x--x 512 -r-x--x-w- 513 -r-x--x-wx 514 -r-x--xr-- 515 -r-x--xr-x 516 -r-x--xrw- 517 -r-x--xrwx 520 -r-x-w---- 521 -r-x-w---x 522 -r-x-w--w- 523 -r-x-w--wx 524 -r-x-w-r-- 525 -r-x-w-r-x 526 -r-x-w-rw- 527 -r-x-w-rwx 530 -r-x-wx--- 531 -r-x-wx--x 532 -r-x-wx-w- 533 -r-x-wx-wx 534 -r-x-wxr-- 535 -r-x-wxr-x 536 -r-x-wxrw- 537 -r-x-wxrwx 540 -r-xr----- 541 -r-xr----x 542 -r-xr---w- 543 -r-xr---wx 544 -r-xr--r-- 545 -r-xr--r-x 546 -r-xr--rw- 547 -r-xr--rwx 550 -r-xr-x--- 551 -r-xr-x--x 552 -r-xr-x-w- 553 -r-xr-x-wx 554 -r-xr-xr-- 555 -r-xr-xr-x 556 -r-xr-xrw- 557 -r-xr-xrwx 560 -r-xrw---- 561 -r-xrw---x 562 -r-xrw--w- 563 -r-xrw--wx 564 -r-xrw-r-- 565 -r-xrw-r-x 566 -r-xrw-rw- 567 -r-xrw-rwx 570 -r-xrwx--- 571 -r-xrwx--x 572 -r-xrwx-w- 573 -r-xrwx-wx 574 -r-xrwxr-- 575 -r-xrwxr-x 576 -r-xrwxrw- 577 -r-xrwxrwx 600 -rw------- 601 -rw------x 602 -rw-----w- 603 -rw-----wx 604 -rw----r-- 605 -rw----r-x 606 -rw----rw- 607 -rw----rwx 610 -rw---x--- 611 -rw---x--x 612 -rw---x-w- 613 -rw---x-wx 614 -rw---xr-- 615 -rw---xr-x 616 -rw---xrw- 617 -rw---xrwx 620 -rw--w---- 621 -rw--w---x 622 -rw--w--w- 623 -rw--w--wx 624 -rw--w-r-- 625 -rw--w-r-x 626 -rw--w-rw- 627 -rw--w-rwx 630 -rw--wx--- 631 -rw--wx--x 632 -rw--wx-w- 633 -rw--wx-wx 634 -rw--wxr-- 635 -rw--wxr-x 636 -rw--wxrw- 637 -rw--wxrwx 640 -rw-r----- 641 -rw-r----x 642 -rw-r---w- 643 -rw-r---wx 644 -rw-r--r-- 645 -rw-r--r-x 646 -rw-r--rw- 647 -rw-r--rwx 650 -rw-r-x--- 651 -rw-r-x--x 652 -rw-r-x-w- 653 -rw-r-x-wx 654 -rw-r-xr-- 655 -rw-r-xr-x 656 -rw-r-xrw- 657 -rw-r-xrwx 660 -rw-rw---- 661 -rw-rw---x 662 -rw-rw--w- 663 -rw-rw--wx 664 -rw-rw-r-- 665 -rw-rw-r-x 666 -rw-rw-rw- 667 -rw-rw-rwx 670 -rw-rwx--- 671 -rw-rwx--x 672 -rw-rwx-w- 673 -rw-rwx-wx 674 -rw-rwxr-- 675 -rw-rwxr-x 676 -rw-rwxrw- 677 -rw-rwxrwx 700 -rwx------ 701 -rwx-----x 702 -rwx----w- 703 -rwx----wx 704 -rwx---r-- 705 -rwx---r-x 706 -rwx---rw- 707 -rwx---rwx 710 -rwx--x--- 711 -rwx--x--x 712 -rwx--x-w- 713 -rwx--x-wx 714 -rwx--xr-- 715 -rwx--xr-x 716 -rwx--xrw- 717 -rwx--xrwx 720 -rwx-w---- 721 -rwx-w---x 722 -rwx-w--w- 723 -rwx-w--wx 724 -rwx-w-r-- 725 -rwx-w-r-x 726 -rwx-w-rw- 727 -rwx-w-rwx 730 -rwx-wx--- 731 -rwx-wx--x 732 -rwx-wx-w- 733 -rwx-wx-wx 734 -rwx-wxr-- 735 -rwx-wxr-x 736 -rwx-wxrw- 737 -rwx-wxrwx 740 -rwxr----- 741 -rwxr----x 742 -rwxr---w- 743 -rwxr---wx 744 -rwxr--r-- 745 -rwxr--r-x 746 -rwxr--rw- 747 -rwxr--rwx 750 -rwxr-x--- 751 -rwxr-x--x 752 -rwxr-x-w- 753 -rwxr-x-wx 754 -rwxr-xr-- 755 -rwxr-xr-x 756 -rwxr-xrw- 757 -rwxr-xrwx 760 -rwxrw---- 761 -rwxrw---x 762 -rwxrw--w- 763 -rwxrw--wx 764 -rwxrw-r-- 765 -rwxrw-r-x 766 -rwxrw-rw- 767 -rwxrw-rwx 770 -rwxrwx--- 771 -rwxrwx--x 772 -rwxrwx-w- 773 -rwxrwx-wx 774 -rwxrwxr-- 775 -rwxrwxr-x 776 -rwxrwxrw- 777 -rwxrwxrwx |
Let us know if you face any problem running above program.