Search code examples
javacompiler-errorsfile-permissionspermission-denied

Permission denied when trying to compile java files with another class from another directory


For context, there are 2 java package files, Poligon.java and PersegiPanjang.java. I was trying to compile my second package but it said permission denied. I've checked the directory permission and it permitted all permission, but yet still denied when I tried to compile the package.

mylaptop@MacBook-Pro pert 3 % javac org/poligon/Poligon.java                             
mylaptop@MacBook-Pro pert 3 % javac -cp org/poligon;. org/bangundatar/PersegiPanjang.java
error: no source files
org/bangundatar/PersegiPanjang.java:1: permission denied: /Applications
org/bangundatar/PersegiPanjang.java:2: command not found: 3-inheritance.pdf
org/bangundatar/PersegiPanjang.java:3: command not found: 3-inheritance.pdf
org/bangundatar/PersegiPanjang.java:4: command not found: 3-inheritance.pdf
org/bangundatar/PersegiPanjang.java:5: permission denied: org/
org/bangundatar/PersegiPanjang.java:7: command not found: package
org/bangundatar/PersegiPanjang.java:9: command not found: import
org/bangundatar/PersegiPanjang.java:11: command not found: public
org/bangundatar/PersegiPanjang.java:private:12: not valid in this context: panjang,lebar
double=''

Poligon.java is located in "/Users/username/Desktop/Matkul/smt 4/pbo/prak/pert 3/org/poligon".

PersegiPanjang.java is located in "/Users/username/Desktop/Matkul/smt 4/pbo/prak/pert 3/org/bangundatar".

Poligon.java:

package org.poligon;

public class Poligon{
    protected int jumlahSisi;

    public int getJumlahSisi(){
        return this.jumlahSisi;
    }
}

PersegiPanjang.java:

package org.bangundatar;

import org.poligon.Poligon;

public class PersegiPanjang extends Poligon{
    private double panjang,lebar;

    public PersegiPanjang(double panjang, double lebar, int jumlahSisi){
        this.panjang = panjang;
        this.lebar = lebar;
        this.jumlahSisi = jumlahSisi;
    }

    public double hitungLuas(){
        return panjang * lebar;
    }

    public void printInfo(){
        System.out.println("Bangun Persegi Panjang bersisi         "+this.getJumlahSisi());
    }
}

I'm expecting it to be successfully compiled without any denied permission.This is the file permission access information


Solution

  • You're using the wrong guide; you are using one for windows, which means you are now using the wrong java path separator, which means this command is parsed differently.

    javac -cp org/poligon;. org/bangundatar/PersegiPanjang.java

    on the windows operating system, : is the separator for drive letters, so it cannot be used to separate paths, so on windows, ; is used. But in virtually all command line shells (Except windows's), ; is the command separator. So, : is used. The above, when you type that into a terminal, means bash has a look at that and goes:

    Right, you want me to first run:

    javac -cp org/poligon
    

    and then you want me to run:

    . org/bangundatar/PersegiPanjang.java
    

    And that is exactly what your output says is happening. The first line produces the error 'no source files'.

    The second line takes some more explaining: Almost all shell applications (it's the app you are typing these commands into, the one with the white text in the black box) have a source command, which reads in any file you provide it, and executes it like a shell script: Just run every command in that text file as if you typed it on the command line yourself, directly. And most shells have a single . as an alias for it.

    So, . org/bangundatar/PersegiPanjang.java is identical to opening that java file, selecting all of it, copying it to the clipboard, going to your terminal, and pasting it all. Which results in a bevy of errors, as java, obviously, isn't command line speak.

    The fix? Just replace that ; with :. More general, you should always put java path args in single quotes, because if your shell escapes or otherwise processes it, it's never going to do what you want. javac -cp 'org/poligon:.' org/bangundatar/*.java is the command you want to type.