Reputation: 2378
Given the following codes I use to gunzip a '.gz' file:
public class TestCSVDataToMap {
private static final String INPUT_GZIP_FILE = "/opt/old.csv.gz";
private static final String OUTPUT_FILE = "/opt/new.csv";
public static void main(String[] args) throws IOException{
TestCSVDataToMap dm = new TestCSVDataToMap();
dm.gunzipIt();
}
public void gunzipIt(){
byte[] buffer = new byte[1024];
try{
GZIPInputStream gzis =
new GZIPInputStream(new FileInputStream(INPUT_GZIP_FILE));
FileOutputStream out =
new FileOutputStream(OUTPUT_FILE);
int len;
while ((len = gzis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
gzis.close();
out.close();
System.out.println("Done");
}catch(IOException ex){
ex.printStackTrace();
}
}
}
throws out such an exception:
java.io.FileNotFoundException: /opt/new.csv (Permission denied)
this is because the path '/opt' are not allowed to make any changes. But I can use
sudo gunzip /opt/old.csv.gz
to unzip the file. So in Java, what should I do to act like a 'sudo'?
Upvotes: 0
Views: 1112
Reputation: 36431
There is no standard way to sudo
in Java. But you can sudo
your Java program sudo java TestCSVDataToMap
.
Java isn't designed to manage OS process access rights.
Upvotes: 1
Reputation: 201497
Start your java
process with sudo
sudo java -jar myjar.jar
Or, write to a folder you do have permission to write to (like /tmp
or $HOME
).
Upvotes: 0