Cannot read file FileNotFoundException - Access is denied - java

I'm getting "Access is denied" when reading a file from my Java program on Windows Server 2012. I've been doing this sort of thing for years so I'm not a newbie. I just can't figure out what I'm missing!
Here is the stack (edited):
Caused by: java.io.FileNotFoundException: C:/ProgramData/MyProgram/resource/file.license (Access is denied)
#0: java.io.FileInputStream.open0(Native Method)
#1: java.io.FileInputStream.open(Unknown Source)
#2: java.io.FileInputStream.<init>(Unknown Source)
#3: java.io.FileInputStream.<init>(Unknown Source)
#4: com...util.FileUtil.readFileAsString(FileUtil.java:269)
The Java program is being run as a Windows Service using NSSM. The service is configured to run as user "cmb#contoso.com". The "file.license" file has user cmb#contoso.com with "Full" access. Domain "Users" group has Read, Read & Execute perms.
The perms on "C:/ProgramData/MyProgram" grant cmb#contoso.com Full access.
If I run Process Explorer and look at the "java.exe" Properties > Security I see that it shows "CONTOSO\cmb" as the user the process is running as.
I tried granting "Everyone" Read, Read & Execute perms on C:\ProgramData\MyProgram and on file.license but that had no effect.
If I run this same code direct, say from Eclipse, it works fine.
The readFileAsString method:
public static String readFileAsString(String filePath) {
if (filePath == null)
throw new IllegalArgumentException("No file argument given");
try {
byte[] buffer = new byte[(int) new File(filePath).length()];
FileInputStream f = new FileInputStream(filePath);
f.read(buffer);
f.close();
return new String(buffer);
} catch (IOException e) {
throw new OperationFailedException(e);
}
}
Java is 1.8_111 from Oracle
Process Monitor trace is shown in screenshot:
https://drive.google.com/file/d/0B8BMXJDodRtpY19VekRaTkR5bTA/view
Process Monitor security properties of "java.exe" screenshot:
https://drive.google.com/file/d/0B8BMXJDodRtpQko0YlVRNkZBQ1k/view

Related

what could be the reason for a file on mapped network drive not being seen by java?

My code is:
File file = new File( "T:\\sub1\\15a12f0103135510.jpg");
long lastModified = file.lastModified();
System.out.println( "last modified: " + lastModified);
System.out.println( "done: " + file.exists());
Last modified returns 0. file.exists returns false. However, I can access and open the file through windows explorer, no problem. Accessing the network share directly doesn't work either (in code). Tried smb - shows errors too. I am the admin user on the machine. What could possibly be the reason? what can I check?
if I do the following:
URL url = new URL( "file://host/share/sub1/15a12f0103135510.jpg");
url.openStream();
I get the following error:
java.io.IOException: sun.net.ftp.FtpProtocolException: Welcome message: 501 Proxy unable to contact ftp server
at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:308)
at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnection.java:400)
at java.net.URL.openStream(URL.java:1045)
at apiteest.ApiTest.main(ApiTest.java:24)
Caused by: sun.net.ftp.FtpProtocolException: Welcome message: 501 Proxy unable to contact ftp server
at sun.net.ftp.impl.FtpClient.connect(FtpClient.java:1022)
at sun.net.ftp.impl.FtpClient.connect(FtpClient.java:1005)
at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:294)
... 3 more
Suppressed: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
at sun.net.ftp.impl.FtpClient.readServerResponse(FtpClient.java:420)
at sun.net.ftp.impl.FtpClient.readReply(FtpClient.java:497)
at sun.net.ftp.impl.FtpClient.issueCommand(FtpClient.java:539)
at sun.net.ftp.impl.FtpClient.close(FtpClient.java:1131)
at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:303)
... 3 more
Exception in thread "main" java.io.IOException: sun.net.ftp.FtpProtocolException: Welcome message: 501 Proxy unable to c
ontact ftp server
Update:
the code works when executed from powershell but not when executed from powershell started as administrator (the same user though). it also doesn't work from a service, even though the service runs under the same user. Why is it so? And how to enable the share (and possibly the mapped drive) for the service?

matlabcontrol fails to start MATLAB when executed from jar-file

I am using matlabcontrol to start matlab-sessions and everything works fine if i start it from eclipse. A new Matlab-session is opened or, if one is already open&connected, the old one is reactivated.
However, I want to do this via a windows command line that is called from a python program.
If I compile and export this to a .jar-file, it still works, but ONLY if an old, manually started MATLAB session is still open.
my current cmd-command is simply:
java -jar StartmatlabSim.jar
If no session is open, i get the following error in the cmd:
C:\Users\rEngel\Desktop>java -jar StartmatlabSim.jar
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: matlabcontrol.MatlabConnectionException: Support code location could not be determined. Could not ge
t path from URI location.
URI Location: jar:rsrc:matlabcontrol-4.1.0.jar!/
URL Location: jar:rsrc:matlabcontrol-4.1.0.jar!/
Code Source: (jar:rsrc:matlabcontrol-4.1.0.jar!/ <no signer certificates>)
Protection Domain: ProtectionDomain (jar:rsrc:matlabcontrol-4.1.0.jar!/ <no signer certificates>)
java.net.URLClassLoader#677327b6
<no principals>
java.security.Permissions#27973e9b (
("java.security.AllPermission" "<all permissions>" "<all actions>")
)
Class Loader: java.net.URLClassLoader#677327b6
Class Loader Class: class java.net.URLClassLoader
at matlabcontrol.Configuration.getSupportCodeLocation(Configuration.java:295)
at matlabcontrol.RemoteMatlabProxyFactory.createProcess(RemoteMatlabProxyFactory.java:289)
at matlabcontrol.RemoteMatlabProxyFactory.requestProxy(RemoteMatlabProxyFactory.java:125)
at matlabcontrol.RemoteMatlabProxyFactory.getProxy(RemoteMatlabProxyFactory.java:144)
at matlabcontrol.MatlabProxyFactory.getProxy(MatlabProxyFactory.java:81)
at StartMatlab.main(StartMatlab.java:14)
... 5 more
EDIT: For some reason, the system seems to treat a manually started session different from a commandline-started session. Strangely, a batch file with only that single command line also works, but not if it is called from python - it must be clicked on or executed from cmd (!?!?!?).
I am totally lost here....
Beeing able to run and launch my MATLAB-sessions is important for me, because I need to include it into a python-program (mosaik), which uses a cmd-command to start java.
If anybody recognizes the issue and knows a fix I would be very grateful.
Thank you&regards,
Robin
P.S.:
The Java-code is:
import matlabcontrol.*;
public class StartMatlab {
public static void main(String[] args)
throws MatlabConnectionException, MatlabInvocationException
{
// create proxy
MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()
.setUsePreviouslyControlledSession(true).setHidden(true)
.build();
MatlabProxyFactory factory = new MatlabProxyFactory(options);
MatlabProxy proxy = factory.getProxy();
// call builtin function
proxy.eval("disp('hello world')");
// close connection
proxy.disconnect();
}
}
Also, I included matlabcontrol-4.1.0.jar to the .jar build path and chose to package the library into the jar file during export.

ENOENT (No such file or directory) When There Is A File There

I am trying to share A PNG with ShareActionProvider in Android. When I open the PNG to the Uri, it says there is no file found.
open failed: ENOENT (No such file or directory) even though I have gone into the file system and have seen it myself. I have tried it on my phone and the AVD with the save error. I have looked around but have found no answers. Any help would be much appreciated.
Here is where I try to open the file:
File file = new File(getFilesDir()+"wifiqr/", "QRCode.png");
file.setReadable(true, false);
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM,uri);
provider.setShareIntent(intent);
If it helps here is where I save it:
String fileName = getFilesDir()+"/wifiqr/" + "QRCode.png";
etSSID.setText(fileName);
OutputStream stream = null;
try {
stream = new FileOutputStream(fileName);
bmp.compress(Bitmap.CompressFormat.PNG, 80, stream);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Finally the error log:
11-15 02:34:43.243 594-892/com.android.mms E/Mms/media: IOException caught while opening or reading stream
java.io.FileNotFoundException: /data/data/com.frostbytedev.wifiqr/fileswifiqr/QRCode.png: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:416)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at java.io.FileInputStream.<init>(FileInputStream.java:105)
at android.content.ContentResolver.openInputStream(ContentResolver.java:447)
at com.android.mms.model.MediaModel.initMediaSize(MediaModel.java:235)
at com.android.mms.model.MediaModel.<init>(MediaModel.java:74)
at com.android.mms.model.RegionMediaModel.<init>(RegionMediaModel.java:36)
at com.android.mms.model.RegionMediaModel.<init>(RegionMediaModel.java:31)
at com.android.mms.model.ImageModel.<init>(ImageModel.java:73)
at com.android.mms.ui.SlideshowEditor.changeImage(SlideshowEditor.java:163)
at com.android.mms.data.WorkingMessage.internalChangeMedia(WorkingMessage.java:640)
at com.android.mms.data.WorkingMessage.changeMedia(WorkingMessage.java:588)
at com.android.mms.data.WorkingMessage.setAttachment(WorkingMessage.java:453)
at com.android.mms.ui.ComposeMessageActivity.addImage(ComposeMessageActivity.java:3150)
at com.android.mms.ui.ComposeMessageActivity.addAttachment(ComposeMessageActivity.java:3291)
at com.android.mms.ui.ComposeMessageActivity.access$5900(ComposeMessageActivity.java:167)
at com.android.mms.ui.ComposeMessageActivity$35.run(ComposeMessageActivity.java:3236)
at com.android.mms.ui.AsyncDialog$ModalDialogAsyncTask.doInBackground(AsyncDialog.java:129)
at com.android.mms.ui.AsyncDialog$ModalDialogAsyncTask.doInBackground(AsyncDialog.java:84)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:400)
... 24 more
The getFilesDir() is private to that specific app (i.e., other apps cannot read it) as explained in openFileOutput (which is the same directory getFilesDir returns as per its documentation).
If you are attempting to share files across applications, you follow the Sharing Files training guide to ensure that other applications can access your files.
In your save code you have:
String fileName = getFilesDir()+"/wifiqr/" + "QRCode.png";
In your load code you have no leading slash before "wifiqr/":
File file = new File(getFilesDir()+"wifiqr/", "QRCode.png");
It seems you meant to put a "/" before "wifiqr/" in your load code. Your save code implies that getFilesDir() may not already have a trailing slash on it.
The name of the file it is trying to open is even printed in your log:
/data/data/com.frostbytedev.wifiqr/fileswifiqr/QRCode.png
It doesn't seem like you expect to have a directory named "fileswifiqr".
The missing link in your troubleshooting attempt was: While you knew the file you were intending to open existed, you still needed to make sure that your code was actually opening the file you were intending to open.

Lotus Notes Agent - Java Security Error

I have a Lotus Notes Agent written in Java which should pop up a message to the user after it is executed by clicking a button in the Notes client. Trying to display the pop-up causes an error which I see in the Java Debug console as :
java.security.AccessControlException: Access denied (java.lang.RuntimePermission exitVM.0)
at java.security.AccessController.checkPermission(AccessController.java:108)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:544)
at COM.ibm.JEmpower.applet.AppletSecurity.superDotCheckPermission(AppletSecurity.java:1449)
at COM.ibm.JEmpower.applet.AppletSecurity.checkRuntimePermission(AppletSecurity.java:1311)
at COM.ibm.JEmpower.applet.AppletSecurity.checkPermission(AppletSecurity.java:1611)
at COM.ibm.JEmpower.applet.AppletSecurity.checkPermission(AppletSecurity.java:1464)
at java.lang.SecurityManager.checkExit(SecurityManager.java:756)
at java.lang.Runtime.exit(Runtime.java:99)
at java.lang.System.exit(System.java:279)
at ClipboardTest.main(Unknown Source)
at JavaAgent.NotesMain(Unknown Source)
at lotus.domino.AgentBase.runNotes(Unknown Source)
at lotus.domino.NotesThread.run(Unknown Source)
My agent code is
import lotus.domino.*;
import javax.swing.JOptionPane;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
// (Your code goes here)
Document cdoc = agentContext.getDocumentContext();
String[] notesURL = new String[] {cdoc.getNotesURL()};
ClipboardTest.main(notesURL);
JOptionPane.showMessageDialog(null,"message","title",JOptionPane.WARNING_MESSAGE);
} catch(Exception e) {
e.printStackTrace();
}
}
}
I have manager access to the Database. What could be causing this error?
First look at what the error actually is:
Access denied (java.lang.RuntimePermission exitVM.0)
You are telling the VM to die and you do not have the rights to do this. If you had, you would cause anything else running on that JVM instance to die as well, possibly leading to a hang/crash.
Secondly your code is failing in the ClipboardTest.main() method, which you haven't posted any code for.
In the agent properties there is an option to add debug data. This will give you the exact line number that is causing the issue.
My guess is you have a System.exit() call in the ClipboardTest.main() . It shouldn't be there.

Java file input and output

I have the below method which is meant to append information to a file but I get the error below. In the method I use parts of robocode API which inherits from java.io.InputStream
All the permissions on the files and folders seem fine and the file does exist
static public void logInfo(String info)
{
RobocodeFileWriter in;
try {
in = new RobocodeFileWriter("log.txt");
in.append(info);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
SYSTEM: An error occurred during initialization of itc.solomon
SYSTEM: java.security.AccessControlException: Preventing itc.solomon from access: (java.io.FilePermission log.txt read): You may only read files in your own root package directory.
java.security.AccessControlException: Preventing itc.solomon from access: (java.io.FilePermission log.txt read): You may only read files in your own root package directory.
at robocode.security.RobocodeSecurityManager.handleSecurityProblem(Unknown Source)
at robocode.security.RobocodeSecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkRead(SecurityManager.java:888)
at java.io.File.exists(File.java:748)
at robocode.RobocodeFileOutputStream.(Unknown Source)
at robocode.RobocodeFileOutputStream.(Unknown Source)
at robocode.RobocodeFileWriter.(Unknown Source)
at itc.CFile.logInfo(CFile.java:16)
at itc.solomon.(solomon.java:43)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at robocode.peer.proxies.HostingRobotProxy.loadRobotRound(Unknown Source)
at robocode.peer.proxies.HostingRobotProxy.run(Unknown Source)
at java.lang.Thread.run(Thread.java:636)
See this sample:
http://code.google.com/p/robocode/source/browse/robocode/trunk/robocode.samples/src/main/java/sample/SittingDuck.java
I think getDataFile("log.txt") will give you proper location.
I've searched a bit about this problem. It seems that there's a bug in the robocode package, awkwardly solved by:
while (!dataIsLoaded) {
try {
tryToReadData;
dataIsLoaded = true;
} catch (AnyException e) {}
}
It's a quite awful way to solve the problems, for many reasons (ignoring exceptions, busy-waiting, etc.) A more sane way would be downgrading the robocode package to a previous, more stable version.
See search results here.

Categories

Resources