I have installed wkhtmltopdf utility and its accessible via mac terminal. But when I'm trying to access it via java code, I get the following error
Cannot run program "wkhtmltopdf": error=2, No such file or directory
I am using this wrapper of wkhtmltopdf https://github.com/jhonnymertz/java-wkhtmltopdf-wrapper
Same code is perfectly running fine in windows system. So i believe issue is something related to tomcat not able to access the wkhtmltopdf utility.
Here is the code that i am using,
Pdf pdf = new Pdf();
pdf.addPage(serverBasePath + "/htmlview", PageType.url);
// Save the PDF
pdf.saveAs(filePath + "\\" + filename);
You need to tell java-wkhtmltopdf-wrapper were the actual program is on the disk. Try this:
WrapperConfig wc = new WrapperConfig("C:\\Program Files\\wkhtmltopdf\\wkhtmltopdf.exe");
Pdf pdf = new Pdf(wc);
...
pdf.saveAs(...);
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 have a java program that use PKCS11 and read digital certificates from e-token. This program uses libpkcs11wrapper.dll file. Program runs perfectly fine on local machine. But when I access it via server it gave me error that libpkcs11wrapper.dll file not found. So my question is how I can load the dll file on client machine from server? I have signed the applet. And here is the code which is giving error
File jarPath = new File(applicationPath);
if (!jarPath.isDirectory()) {
jarPath = jarPath.getParentFile();
}
File pkcs11wrapperFile = new File(jarPath, "libpkcs11wrapper" + arch + ".dll");
System.out.println("Absolute path is "+pkcs11wrapperFile.getAbsolutePath());
Pkcs11Shell pkcs11Shell = new Pkcs11Shell(pkcs11wrapperFile.getAbsolutePath());
I have developed a Play framework application on my windows PC and then transferred it onto my Linux box, I'm uploading a video and a photo to the server, this upload process works perfectly on my Windows PC, but doesn't work on the Linux box.
code I'm using in windows:
String root = Play.application().path().toString();
String globalFolderPath = root + "/public/globalUploadFolder/";
File globalFolder = new File(globalFolderPath);
Code I tried in Linux as well as the above code:
String globalFolderPath = "../../public/globalUploadFolder/";
File globalFolder = new File(globalFolderPath);
Is there a something I have to do regarding file paths differently on the Linux box, could it be a permission issue?
I'm lost as to why this is happening.
The problem was solved by using File.separator
It seems that my code won't work after learning that the machine that I'll be pointing the upload path to is a Linux box.
My use case is, a user logs in to the web app, chooses a file to upload, then click upload button. Is it possible to do this direct from the Java code to the Linux server using appropriate ssh or scp libraries if there is any?
EDIT: Here's my current code.
#Override
public void fileTransfer(File uploadedFile, String fileName, String pathTemp) {
File destFile = new File( pathTemp + File.separator + fileName);
try{
FileUtils.copyFile(uploadedFile, destFile);
String getTempFile = destFile.toString();
String tempPath = getTempFile.replace("\\", "\\\\");
File tempFile = new File(tempPath); // 1st file
String tempFileName = tempFile.getName();
String fileSave = getUploadPathSave().replace("\\", "\\\\");
tempFile.renameTo(new File(fileSave + tempFileName));
} catch (IOException ex) {
System.out.println("Could not copy file " + fileName);
ex.printStackTrace();
}
}
If your app is deployed at one place only (not mass distribution), the easiest way would be:
create samba share on linux machine
map samba share to logical drive on windows machine
do usual file copy with java functions.
attention: renameTo will not work between drives. You'll need to copy input stream to output stream or, better, use apache commons-io functions for that.
There are different possibilities:
If you can create a shared directory in linux and mount it under windows (see Samba. Then you can write to that directory like a local directory. File will go to the linux server.
Use a library like Jsch to upload the file from windows server to linux server.
There are certain things you can do:
1-> If you can program your linux server, then you can make a program that listens to user request on a port, and stores data in file. Then you can send you files to that port of server.
2-> The other way is you can use some sort of script to create ssh-connection to server and then you can just add file through ssh, but here your java program will not be useful.
I personally use my own program to share files between 2 machines in same network.
You can use it,if it will be useful for you: https://github.com/RishabhRD/xshare
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();