access denied to access to /etc/hosts - java

i can't access to hosts file of windows with java (just write) :
java.io.FileNotFoundException: C:\Windows\system32\drivers\etc\hosts (Access is denied)
at java.io.FileOutputStream.open(Native Method)
when i want append my text to hosts file get above error ...
this is my code :
BufferedWriter bw = null;
try {
// APPEND MODE SET HERE
bw = new BufferedWriter(new FileWriter(file,true));
bw.write(text);
bw.newLine();
bw.flush();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally { try {
// always close the file
bw.close();
} catch (IOException ex) {
Logger.getLogger(FileWrite.class.getName()).log(Level.SEVERE, null, ex);
}
}
} // end test()
this code work for simple files in other location of windows .. (just get error on c:\windows\ ...
and :
i use microsoft manifest to set administration access [not work]
set full access to hosts file [not work]
please help ..
tnx

Check if your java is really running as administrator (i.e. write to a new file in the same folder). There is obviously access problem and to solve it you need to understand who you are (user/service of your app) and what are permissions of resource (file) you are trying to access.
here are two ways how you can try and debug it:
1) You don't have permissions to write the file (can you read it?)
2) You don't have permissions to enter the folder (can you list files in it)

Related

Where could I save a log file in a server using web wervice JAVA (.war)?

I create a web service using JAVA. I want to create a log file in the server to recover it later but my problem is that I cannot save the file due to client rights.
In my local server, I can save it in the root path, but in the Devian server where I will use it, this is not possible.
I try to change the path, but the problem remains because my local paths are different than other server.
This is my code:
BufferedWriter bw = null;
FileWriter fw = null;
try {
File file = new File(System.getProperty("user.dir")+File.separator+"zlm_"+thoy+".txt");
// Si el archivo no existe, se crea!
if (!file.exists()) {
file.createNewFile();
}
// flag true, indica adjuntar informaciĆ³n al archivo.
fw = new FileWriter(file.getAbsoluteFile(), true);
bw = new BufferedWriter(fw);
bw.write(entrada+";"+porcentaje+";");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//Cierra instancias de FileWriter y BufferedWriter
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex)
{
ex.printStackTrace();
}
}
Any ideas?
I recommend putting it in /var/log or in the /tmp directory. If you still don't have permissions to those directories, you can always put it in the relative/working directory of the war file.
More on how to create files in the working directory in this answer.
Make the path to the log file configurable so the server admin can set it. Ask the admin for a path to test it on.

Android Write file to a different application folder

My device is rooted
The target audience (if app is shared) will be rooted devices only
I'm trying to customize another app slightly by modifying some image files. So objective is to simply overwrite tiny jpg files (one at a time as required)
/data/data/com.otherapp/files/somefile.jpg for example needs to be overwritten
p = Runtime.getRuntime().exec("su"); is already run before I tried the write method.
I also added Runtime.getRuntime().exec("chmod 077 " +myFile.getAbsolutePath()); before the write code as suggested on a similar question
I'm getting permission denied.
java.io.FileNotFoundException: /data/data/com......jpg: open failed: EACCES (Permission denied)
The write code I have is:
//myDir is /data/data/com.......
File myFile = new File (myDir.getAbsolutePath(), fname);
try {
//myFile.createNewFile();//tried with and without this
FileOutputStream out = new FileOutputStream(myFile);
btbmp.compress(Bitmap.CompressFormat.JPEG, 60, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
How can I do this?
Okay it's taken all day but I've achieved the desired result thus:
Create file on SD card
Then copy file to root destination
src_file = "somefile/on/sdcard.jpg"
dest_file = "/data/data/com.anotherapp/files/abcde.jpg" got path using context
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("rm "+dest_file + "\n"); //removes destination file -might not be required - haven't tested
os.flush();
os.writeBytes("cat "+src_file+" > "+dest_file + "\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
// Waits for the command to finish.
process.waitFor();
}
catch (IOException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
Note In some cases I had to chmod the root folder 777 for it to work. I'm sure this isn't good practice and doing so will mean the app that uses that folder might not be able to access it. In my case that's what happened until I rectified it using ES Explorer

chmod command in data pump directory not working second time using Java code deployed on oracle weblogic server

I am trying to create dmp file of a table A_LOCKER using expdp cmd and then sftp it back to local machine on which my java code is deployed.The server is an oracle weblogic server.
The code is working fine for first time but from second time it is giving error as:Permission denied
The dmp file is created in a data pump directory Extract which is create by me.
Now i try to sftp the dmp file and for that i need to provide sudo permission for the file
so i tried following in my java code:
1)To create session:
StringBuffer cmd = new StringBuffer();
FileOutputStream fileOutputStream = null;
SshClient ssh = new SshClient();
SessionChannelClient session = null;
SftpClient sftp = null;
HostKeyVerification host = new IgnoreHostKeyVerification();
// Try to connect to the machine that includes the shared folder
// Throw an exception when a connection is failed.
try {
ssh.connect(machine, host);
} catch (IOException e) {
logger.error("Cannot connect to dbserver in machine "
+ machine,e);
throw new EPCShareFileException(
"Cannot connect to dbserver location in machine "
+ machine, e);
}
PasswordAuthenticationClient auth = new PasswordAuthenticationClient();
auth.setUsername(user);
auth.setPassword(password);
// Authenticate user name and password for connection
// Throw an exception if authentication is failed
try {
ssh.authenticate(auth);
} catch (IOException e) {
logger.error("Cannot authenticate user "
+ user + " " + " password " + password);
ssh.disconnect();
throw new EPCShareFileException("Cannot authenticate user "
+ user + " " + " password " + password, e);
}
2)Execute chmod command using pseudo permission
cmd.append("/usr/local/bin/sudo /bin/chmod -R 777 ")
.append(location+dbDumpFileName);
try{
session = ssh.openSessionChannel();
session.executeCommand(cmd.toString());
} catch (IOException e){
logger.error("Cannot execute chmod cmd");
}
finally{
try {
session.close();
} catch (IOException e) {
// TODO Auto-generated catch block
logger.info("Cannot close the session:"+e.getMessage());
}
}
3)Then i am trying to sftp the dmp file to local server
try {
sftp = ssh.openSftpClient();
} catch (IOException e) {
logger.error("Cannot open connection to database server");
ssh.disconnect();
throw new EPCShareFileException(
"Cannot open connection to database server");
}
try{
fileOutputStream = new FileOutputStream(dbDumpFileName);
sftp.get(location+dbDumpFileName, fileOutputStream);
}catch (IOException e) {
**throw new EPCShareFileException("Cannot retrive file "
+"Error:"+e.getMessage());**
}finally{
try {
fileOutputStream.close();
sftp.quit();
fileOutputStream = null;
sftp = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ssh.disconnect();
ssh = null;
}
For first time when i am deploying my code as ear file on oracle weblogic server it is working file and dmp file is created and sftped to desired location as well
But from second time i am getting error:
Error:Permission denied
Please Help..
First of all, don't ever use chmod -R 777 - that's like publishing your data for anyone with access to the machine, no questions asked. If you used this to debug the issue, then okay, but don't forget to change it back (or get rid of it; you shouldn't need it at all).
To debug "Permission denied" errors, you need to have access to the machine. On the machine, you need to login as the user that got the error (don't try as root or another user).
As this user, try to access every folder in the path that you couldn't access. So if it failed for /srv/www/foo/bar/x/y/z/file.txt, then you need to
ls /srv
ls /srv/www/
ls /srv/www/foo/
ls /srv/www/foo/bar/
ls /srv/www/foo/bar/x/
ls /srv/www/foo/bar/x/y/
ls /srv/www/foo/bar/x/y/z/
ls /srv/www/foo/bar/x/y/z/file.txt
Do it in this order. Find the first command that failed and then check why this user doesn't have permissions for the folder in question.

Attempting to overwrite files results in blank files

In the app I am working on right now, part of the functionality is to write data saved on the device to a flash drive connected via a USB-OTG adapter. Specifically, the device is a rooted Motorola Xoom running 4.2.2. I can successfully write files to the drive and read them on my computer. That part works fine. However, when I try to replace existing files with new information, the resulting files come out empty. I even delete the existing files before writing new data. What's weird is that after copying the contents of my internal file to the flash drive, I log the length of the resulting file. It always matches the input file and is always a non-0 number, yet the file still shows up as blank on my computer. Can anyone help with this problem? Relevant code from the AsyncTask that I have doing this work is below.
#Override
protected Void doInBackground(Void... params) {
File[] files = context.getFilesDir().listFiles();
for (File file : files) {
if (file.isFile()) {
List<String> nameSegments = Arrays.asList(file.getName().split(
"_"));
Log.d("source file", "size: " + file.length());
String destinationPath = "/storage/usbdisk0/"
+ nameSegments.get(0) + "/" + nameSegments.get(1) + "/";
File destinationPathFile = new File(destinationPath);
if (!destinationPathFile.mkdirs()) {
destinationPathFile.mkdirs();
}
File destinationFile = new File(destinationPathFile,
nameSegments.get(2));
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(file);
fw = new FileWriter(destinationFile, false);
int c = fr.read();
while (c != -1) {
fw.write(c);
c = fr.read();
}
fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Log.d("destination file", "size: " + new File(destinationFile.getPath()).length());
}
}
return null;
}
EDIT:
Per #Simon's suggestion, I added output.flush() to my code. This does not change the result.
EDIT #2:
I did some further testing with this and found something interesting. If I go to Settings->Storage->Unmount USB Storage after writing to the flash drive but before removing it from the OTG adapter, everything works perfectly. However, failing to eject the drive after writing results in the data not being written. What's strange is that the folder structure and file itself are created on the drive, but the file is always empty. One more thing: if I go to a file manager application and open up the file prior to removing the drive, the files all exist as they should. However, even removing the device, plugging it straight back in to the tablet and opening any of the files results in the file looking empty. I can't make heads or tails of this, and this is incredibly frustrating. Can anyone help with this?
EDIT #3:
I also changed to using FileReaders and FileWriters just to wee what would happen. I don't care about efficiency at this point, I simply want file writing to work reliably. This change did not affect the issue. Updated code is posted above.
Try using FileReader.ready() method before your FileReader.read() call,
and ensure if your FileReader really has some bytes in it.
Try this , Used buffered reader for writing
try
{
fw = new FileWriter(destinationFile);
BufferedWriter writer=new BufferedWriter(fw);
writer.append(yourText); // Append can be changed to write or something if you want to overwrite
writer.close();
}
catch (Exception e) {
throw new RuntimeException(e);
}
finally {
if (fw != null) {
try {
fw.flush();
fw.close();
}
catch (IOException e) {
}
I found the solution to my problem. It appears that the Android system buffers some files off of the SD card/flash drive, and then writes them to the flash drive upon eject. The following code after my file operations synchronizes the buffer with the filesystem and allows the flash drive to be immediately removed from the device without data loss. It's worth noting that this DOES require root access; it will not work on a non-rooted device.
try {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("sync; sync\n");
os.writeBytes("exit\n");
os.flush();
} catch (Exception e) {
e.printStackTrace();
}
Source of my solution: Android 2.1 programatically unmount SDCard
It sounds like the filesystem is caching your changes, but not actually writing them to the flash drive until you eject it. I don't think there's a way to flush the filesystem cache, so the best solution seems to be just to unmount and then remount the flash drive.

Writing to file from within a servlet(Deployer:Tomcat)

I am using the following code to write to a file from a servlet in Tomcat container. I don't worry if the file gets overwritten during each deploy.
BufferedWriter xml_out = null;
try {
xml_out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(getServletContext().getRealPath("/")
+ File.separator + "WEB-INF" + File.separator
+ "XML.xml"), "UTF8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
xml_out.write(xml);
xml_out.flush();
xml_out.close();
} catch (IOException e) {
e.printStackTrace();
}
However, the file writing is not successful (it doesn't get written in the hard disk). Also, there isn't any exception that gets caught! Is there some security thing in Tomcat that is preventing the file from being written ?
Your code has both "/" and the windows file separator at the start of the filename passed to getRealPath(), Java interprets slashes in filenames according to the current OS.
Not using the file separators might give a better result:
String filename = getServletContext().getRealPath("/WEB-INF/XML.xml");
log.debug("Using XML file: " + filename);
xml_out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename),"UTF8"));
Using a separate variable for the filename lets you log it so you can see unexpected results early in de development process.
I has the same issue.
this link helped me to understand where the file was in the hard disk.
Specifically for me, it put the file in the location that is returned by this line:
System.getProperty("user.dir");
In my case the location was: C:\Users\Myself\programming\eclipse for java

Categories