I got some errors in my app. The app is using applet for accessing local file system.
converting file, for example.
First: Using relative path.
I can place project folder wherever I need and the application still working properly. However, when I placed it on the tomcat server directory, it cannot find the files' path.
Run with browser: file:///C:/report-param/index.html, ok
Run with browser: localhost:8080/report-param/index.html, error
Second: Using absolute path.
The application working well in both browser and tomcat server.
Run with browser: file:///C:/report-param/index.html, ok
Run with browser: localhost:8080/report-param/index.html, ok
Here is my sample code:
File jrxmlPath = new File("reports//people-report-withparam-multi.jrxml");
File pdfPath = new File("reports//people-report-withparam-multi.pdf");
String sourceJrxml = jrxmlPath.getPath();
String destPdf = pdfPath.getPath();
JasperReport jasperReport = JasperCompileManager.compileReport(sourceJrxml);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, paramMap,
getPostgreSQLDataSource(driverUrl, username, password));
JasperExportManager.exportReportToPdfFile(jasperPrint,destPdf);
What is the matter on here? Any ideas?
How can I fix the error above? Please help...
Note: if you don't understand, feel free to ask me...
Related
I'm using IntelliJ and Spring and Java to locally develop an app on a Mac, and then deploy to a tomcat server on AWS, using Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-1048-aws x86_64).
I'm having trouble specifying the file path so that it works in both environments.
My code is
InputStream fileStream = new FileInputStream("src/main/resources/static/web/data/ReportDates.json");
JsonReader reader = Json.createReader(fileStream);
JsonObject reportDates = reader.readObject();
reader.close();
When I run locally, the file is read in correctly. It is located in:
src/main/resources/static/web/data/ReportDates.json
But when I deploy, that code results in the error message:
java.io.FileNotFoundException: src/main/resources/static/web/data/ReportDates.json (No such file or directory)
The actual location of the file on that machine turns out to be:
/opt/tomcat/webapps/automentor/WEB-INF/classes/static/web/data/ReportDates.json
How can I specify the file path so that it works correctly in both environments?
I have given up on using a single path. #Nicholas Pesa got me thinking -- since I use IDEA, I don't have a fixed WEB-INF folder, so it's easier for me to change the path that should be used than to move the file to a fixed location.
My code now uses:
String filepath = (new File("src/main/resources/static/web/data/ReportDates.json").exists()) ? "src/main/resources/static/web/data/ReportDates.json" : "/opt/tomcat/webapps/automentor/WEB-INF/classes/static/web/data/ReportDates.json";
I am getting this error when I'm using .jrxml file in NetBeans applications
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: \schoolmngt\FirstReport.jrxml (The system cannot find the file specified)
Compi at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:174)
The code id
try{
System.out.println("Compiling report...");
JasperCompileManager.compileReportToFile("/schoolmngt/FirstReport.jrxml");
System.out.println("Filling report...");
JasperFillManager.fillReportToFile("/schoolmngt/FirstReport.jasper",new HashMap(), new JREmptyDataSource());
//JasperRunManager.runReportToHtmlFile("FirstReport.jasper",new HashMap(),new JREmptyDataSource());
JasperRunManager.runReportToPdfFile("/schoolmngt/FirstReport.jasper",new HashMap(),new JREmptyDataSource());
}
catch(Exception ee){ee.printStackTrace();}
My file path is:
D:\Schoolmngt\src\schoolmngt\FirstReport.jrxml
When I run the project this error is coming regularly please tell me the solution.
Relative paths do not start with /. /schoolmngt/FirstReport.jrxml is an absolute path.
If the current drive is D: it will look for D:/schoolmngt/FirstReport.jrxml
I had the same problem, but I didn't have time to resolve it so I just provided the full path (starting with drive letter) and it worked. I know it doesn't really fix the problem properly, but if want to just make it working for now try it.
The path in your code should be src/schoolmngt/FirstReport.jrxml.
I have a self signed applet running in the browser, this applet should create a directory on the client machine using this code.
boolean success = (new File("myDir")).mkdirs();
if (!success) {
System.err.println("Directory creation failed");
}
However, when I run it in the browser (under Apache) and after accepting all the security warnings I can't find myDir directory on my machine.
Am I doing something wrong?
I guess you are not looking at the right place...
Given your code snippet, this directory will be created in the current working directory. To be sure where that is on your machine just try to see what the following code gives out :
System.out.println(System.getProperty("user.dir"));
You're not giving it an absolute path so it's creating myDir in the working directory that the browser runs it in, probably a temp dir, or even a "sandbox" area in some browsers.
Because you run applet in sandbox, so You cannot access into user machine resource.
Please see document:
Applet security
I have created an applet to read some info from a file on the server. I try to access the file using the following code:
Properties Settings = new Properties();
settings.load(new URL(getDocumentBase(), "settings.ini")).openStream());
All of a sudden, the second line is giving me the error:
java.lang.NullPointerException
at java.applet.Applet.getDocumentBase(Unknown Source)
My applet is signed and I access it through my localhost.Why can't I use getDocumentBase anymore?
Btw, I am using Netbeans Web Start option to create the necessary files (jars, html, jnlp) and then move them to my IIS local server.
SOLUTION
I'm loading the ini file from within the jar now:
Properties Settings = new Properties();
URL url = this.getClass().getResource("/myapplet/settings.ini");
settings.load(url.openStream());
At first glance I would expect:
new URL(getCodeBase(), "settings.ini")
as getCodeBase gives the directory URL, getDocumentBase gives the HTML URL.
That it worked previously is astonishing. Maybe the HTML URL ended with ?... and you read the HTML page?
SOLUTION
I'm loading the ini file from within the jar now:
Properties Settings = new Properties();
URL url = this.getClass().getResource("/myapplet/settings.ini");
settings.load(url.openStream());
i am getting a problem
i have deployed a war file, when i run localy through tomcat it works fine but when i run on another system by giveing my system ip and then project folder e.g
http:\192.168.0.145\DllTest it loads the applet but when i click on a button to load the functionality it is throwing an exception
Exception in thread "AWT-EventQueue-3" java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: http:\192.168.0.145:8080\DllTest\lib\jinvoke.dll
while it is working fine localy but not in another system. Please tell me what is the problem.
Is it a rights issue or something else.
You cannot load a DLL on an external host. It has to be an absolute disk file system -as the exception message already hints. Your best bet is to download it manually, create a temp file and load it instead.
File dllFile = File.createTempFile("jinvoke", ".dll");
InputStream input = new URL(getCodeBase(), "lib/jinvoke.dll").openStream();
OuptutStream output = new FileOutputStream(dllFile);
// Write input to output and close streams the usual Java IO way.
// Then load it using absolute disk file system path.
System.loadLibrary(dllFile.getAbsolutePath());
dllFile.deleteOnExit();