I have a little GWT/AppEngine Project which uses RPC. Basically I need to get some data from a XML file that resides on the server. But when I use the RPC to read the file in my server-package I am getting a AccessControlException (access denied). Any ideas what the problem is?
//JAXB powered XML Parser
public PoiList readXML() {
try {
unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(new XMLValidEventHandler());
db = (PoiList) unmarshaller.unmarshal(new File("src/com/sem/server/source.xml"));
} catch (JAXBException e) {
e.printStackTrace();
}
return db;
}
java.security.AccessControlException: access denied (java.io.FilePermission \WEB-INF\classes\com\sem\server read)
cheers hoax
I think the problem is that you're trying to read a file that is not located in your working directory. The guidlines for structuring your code in gwt apps are as follows
Under the main project directory
create the following directories:
src folder - contains production Java source
war folder - your web app; contains static resources as well as compiled output
test folder - (optional) JUnit test code would go here
Try moving the file to the war directory (for example /war/resources/myFile.xml) and then open it by
File myFile = new File(System.getProperty("user.dir") + "/resources/myFile.xml");
Usually, when you load a resource that is located in your classpath, you should't use java.io.File. Why? Because it's very much possible, that there is no real file - the classes are often packaged as a .jar file, or even loaded in a completely different way (very likely in the case of AppEngine, though I don't know the details.)
So, if you want to load it directly from your classpath, you can use:
ClassLoader classLoader =
getClass().getClassLoader(); // Or some other way to
// get the correct ClassLoader
InputStream is = classloader.getResourceAsStream("/com/sem/server/source.xml");
Then you can use the input stream in your unmarshaller.
Related
I am struggling with opening a PDF file as a user's documentation in JavaFX program.
Code works fine whenever I open it using IDE. The problem begins when I build my program and try to open it from distributions folder.
For example, this method works, when I am using IDE:
public void helpButtonOnAction(ActionEvent actionEvent) {
File file = new File("src/main/resources/pl/com/buleek/docs.pdf");
hostServices.showDocument(file.getAbsolutePath());
}
But obviously, when I pack my program with gradle build, it is in a directory:
C:\Users\Matthew\Desktop\xxx\ProgramNameFolder\build\distributions\ProgramName\bin
And from now on, it can't see src/main/recources/pl/com/buleek/docs.pdf, because in ProgramNameFolder there are only bin with ProgramName.bat and lib folders.
Is there a way (after building distributions) to paste another folder inside my ProgramName folder, put there my docs.pdf file, and then set code like:
public void helpButtonOnAction(ActionEvent actionEvent) {
File file = new File("ProgramName/docs/docs.pdf");
hostServices.showDocument(file.getAbsolutePath());
}
Or shall I use some command to pack pdf file with my program?
Your build tool will usually place your resources in a jar. As your PDF file is already in your resources folder, it should be packaged into your application jar by default.
You can read resources from your jar file via YourClass.class.getResourceAsStream(), then write the stream to a temp file using basic Java I/O APIs, specifically the version of Files.copy(...) which takes an input stream as a parameter.
The intermediate step of extracting the PDF file from your jar and writing to a temp file in the file system is necessary because you are using host services to launch an external browser and, unlike java, the external browser will be unable to directly read resources from a jar file.
You can convert the file name of the temporary file to a URL string and show it via HostServices.showDocument(uri) as you are currently doing.
If the browser is capable of displaying PDFs (most are) then the PDF will be displayed in the browser, otherwise, the browser will usually allow the user to download the PDF to a file location of their choice on their machine and open the PDF via an external application.
I have a foo.txt file in my /war folder. I'm trying to open it using the File class, but get an exception:
java.security.AccessControlException: access denied (java.io.FilePermission
/foo.txt read)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
at java.security.AccessController.checkPermission(AccessController.java:549)
...
From reading the docs on static/resource files, it seems like the text file should already be available. Does it need some extra setting to allow reading?:
https://developers.google.com/appengine/docs/java/config/appconfig?hl=en#Static_Files_and_Resource_Files
I'm using the local dev server on a mac.
Thanks
I suggest that if you are going to read data files in your Java code and need a place to put your data files (.txt or .csv), etc -- then do the following:
Go to WEB-INF folder
Create some folder inside of the WEB-INF folder. e.g. WEB-INF\data
Place your files in the above folder. E.g. sample.txt
In your Java IO code, you can then refer to the file via WEB-INF\data\sample.txt
I have an ant script which does lots of stuff but I have been asked to provide jar so it can be run as an app.
I have managed to write the java code to invoke ants programatically but I am having the problem to refrence the build file from the code.
File buildFile = new File(BUILD_FILE);
where BUIlD_File is my build.xml (exists in the main directory).
When I export my proj as Runnable jar it throws an exception (File not found build.xml)
Even if I add the build.xml file into jar still it moans though if I put the build.xml file in the same folder where the jar is then it works fine. (But i dont wanna do this)
Can aynone guide me how can I make sure when I export as jar the build.xml file is added in jar and how can I refrence that file(inside jar) in my code.
Any Object running in the JVM can fetch an InputStream for any resource/file within the JAR as follows:
InputStream is = this.getClass().getResourceAsStream("yourpackage/yourfile.xml");
In your case, it sounds like build.xml isn't in a package so you should be able to use:
InputStream is = this.getClass().getResourceAsStream("build.xml");
I've got a silverlight application that loads a dll file located within the ClientBin folder at run time via a relative Uri. It works great on my local machine, but when deployed on a server here, it seems to constantly fail while trying to load the file:
private void OnAssemblyOpened(object sender, OpenReadCompletedEventArgs e)
{
AssemblyPart asmbPart = new AssemblyPart();
MessageBox.Show(e.ToString());
Assembly asmb = asmbPart.Load(e.Result) // this line causes the exception
...
}
Of course silverlight doesn't give me a useful error - just the usual NotFound nonsense. Is there a step I've missed in deploying this? Permissions or something? The dll file is definitely in the ClientBin folder btw - I've checked that! :)
Another option would be to compress the dll into a zip file, then download the zip file. That way you need not play with the server config.
How to download and unpack a file from a Zip file is given in this answer.
Code in essence would look like this:-
AssemblyPart asmbPart = new AssemblyPart();
var zipRes = new StreamResourceInfo(args.Result, null)
var assemRes = Application.GetResourceStream(zipRes, new Uri("YourAssembly.dll", UriKind.Relative));
Assembly asmb = asmbPart.Load(assemRes.Stream)
Try to use absolute path for deployed application and give your url + path-to-clietbin as path.
You may got error because of invalid path on server machine(if you didn't change it and it's still path of your local machine).
Problem was that I'm running IIS6 and dll's cannot be served out without switching execute permissions on the site to None (which obviously stops the Silverlight app from running) so I was legitimately getting a 404 - who'd have thought!!
I created a virtual directory for my scripts at the top level of my site and stuck the dll in there, switched the execute permissions for the virtual to None, updated the uri to ../scripts/ControlLibraries.dll and job's a goodun!
Actually, just change the execute permissions on your application to Scripts only, instead of Scripts and Executables, should work fine.
I get "Error opening ... file ... (No such file or directory)". However I do not control the file access - a third-party library is trying to open a file in the .war.
It works fine when using an open directory structure on my own computer.
My question is: is normal file access supposed to work within a .war? If not, should I just deploy a directory instead of a .war? Or is there a better way?
Am using glassfish.
.war file should be unpacked by your (application) server, so your third-party lib should operate with files from unpacked .war.