How can a java class locate and load a static pdf file in WebLogic 12c? - pdfbox

I need to load a static PDF document, from a java class, running in my J2EE web app on WebLogic 12c; however although my code works in Tomcat, when trying to run it in WebLogic 12c (WebLogic Server Version: 12.2.1.2.0), I get a server error that the PDF file cannot be found (java.io.FileNotFoundException).
I am using Apache's PDF library, PDFBox version 2.0.8 to load a fillable PDF file that I created, and then populate that fillable PDF with data. My code works fine in Tomcat, but fails to find the pdf file when deployed to WebLogic 12c .
-This appears to be because when an EAR file is deployed to WebLogic 12c, the contents in the WAR file (all of the application code/files, including the fillable PDF file), remain archived up in a jar file that WebLogic creates, instead of exploded.
My application utilizes the standard Maven application structure, so as is standard with all static files, I have put my PDF file in the directory for static resources:
src/main/resources/
In my pom.xml file, I have the following, which builds any pdf files in the /src/main/resources/ folder, into the class path root folder of the WAR file.
<resource>
<directory>${basedir}/src/main/resources/</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.pdf</include>
</includes>
</resource>
When I build the WAR and EAR file, the pdf file does indeed get copied into the root folder of the application's class files.
The following 3 lines of code, work to load the PDF, when my application's EAR file is deployed in Tomcat, but do not in WebLogic 12c (WebLogic Server Version: 12.2.1.2.0).
//this classLoader works for Tomcat, but no in WebLogic 12c
ClassLoader classLoader = getClass().getClassLoader();
File file= new File(classLoader.getResource("myPdfFile.pdf").getFile());
PDDocument document = PDDocument.load(file);
WebLogic 12c produces the following error:
<[ACTIVE] ExecuteThread: '6' for queue:
'weblogic.kernel.Default (self-tuning)'> <> <>
<3902331f-a214-42fe-a6a1-35b3531e4b56-000000a9> <1523985710798>
<[severity-value: 8] [rid: 0] [partition-id: 0] [partition-name:
DOMAIN] >
<[ServletContext#1661988196[app:mmhsrp-ear-4.9.0.1-3 module:/mmhsrp
path:null spec-version:3.1]] Servlet failed with an IOException.
java.io.FileNotFoundException:
C:\Users\shawn.oplinger\AppData\Roaming\JDeveloper\system12.2.1.2.42.161008.1648\DefaultDomain\servers\DefaultServer\tmp_WL_user\mmhsrp-ear-4.9.0.1-3\l27tj7\war\WEB-INF\lib_wl_cls_gen.jar!\myPdfFile.pdf
(The system cannot find the path specified)
-When I browse my file system, to manually try to find the pdf file using the path in the WebLogic I can get as far as this directory:
C:\Users\shawn.oplinger\AppData\Roaming\JDeveloper\system12.2.1.2.42.161008.1648\DefaultDomain\servers\DefaultServer\tmp_WL_user\mmhsrp-ear-4.9.0.1-3\l27tj7\war\WEB-INF\lib\
in that directory is then this file:
_wl_cls_gen.jar
in that jar file is indeed my pdf file:myPdfFile.pdf
-So why can't the classLoader find/load the pdf file?
-does it have anything to do with the pdf file is actually in the archived _wl_cls_gen.jar file, and not exploded?
-Any suggestions on how my java class can load a static pdf file in WebLogic 12c?
Thanks!
Shawn

Your code
//this classLoader works for Tomcat, but no in WebLogic 12c
ClassLoader classLoader = getClass().getClassLoader();
File file= new File(classLoader.getResource("myPdfFile.pdf").getFile());
PDDocument document = PDDocument.load(file);
assumes that the resource is available as an individual file in the file system. As your error message and your search show that is not the case in WebLogic, instead it is contained in a jar file.
Thus, your code should not try and access it via a File object. Instead access via an InputStream is possible and also supported by PDFBox:
InputStream resource = classLoader.getResourceAsStream("myPdfFile.pdf");
PDDocument document = PDDocument.load(resource);
resource.close();

Related

JavaMelody error - Monitoring of sql requests and of jdbc connections in GlassFish v4.1

I want to use JavaMelody to monitor the SQL requested by a Glassfish Application Server. There are step-by-step instructions on https://github.com/javamelody/javamelody/wiki/UserGuideAdvanced#monitoring-of-sql-requests-and-of-jdbc-connections-in-glassfish-v3
I followed the instructions (I didn't download javamelody-objectfactory.jar but used javamelody-core-1.54.0.jar instead) and I get this error when clicking on the refresh button (javamelody web page) :
server.log :
exception while collecting data
java.lang.NoClassDefFoundError: org/jrobin/core/RrdException
at net.bull.javamelody.Collector.getCounterJRobin(Collector.java:836)
at net.bull.javamelody.Collector.collectJRobinValues(Collector.java:489)
...
any idea how to resolve this ?
jrobin-1.5.9.1.jar is installed in the lib folder of glassfish (and in my ear project).
thanks !
javamelody-objectfactory.jar (java source included in the jar) and javamelody-core jar file are absolutely different things. The first is to make the datasource monitorable in Glassfish and the second is the monitoring tool itself.
First fix the exception. You should probably put javamelody-core jar and jrobin jar files in your ear project (and not one in lib folder of Glassfish and one in ear).
Then if the monitoring reports don't include SQL monitoring for the datasource declared in Glassfish, use the javamelody-objectfactory.jar including all steps as said in the doc.

commandline tool Glassfish appclient doesn't see deployed EJB

I'm working my way through a book about "Java EE 7 for Glassfish", with the server installed on Fedora Linux.
I have a simple stateless session bean SimpleSessionBean deployed on the server and I am trying to approach that SimpleSessionBean via SessionBeanClient and the Glassfish command line tool appclient, running a client jar. Everything from the book, so it should work. The client however can't find SimpleSessionBean. Apparently a class path issue. In the server logs nothing happened.
I can't find any pointers how Glassfish should be properly installed. Everything works within the server. I can approach installed war files from facelets running in a browser.
It is probably a matter of setting $PATH right or something or some other environment variable. Any pointers to relevant literature?
Thanks in advance for any suggestions!
UPDATE1: error message
From the bash terminal window where I run appclient:
[fedora#localhost bin]$ ./appclient -client /home/fedora/Downloads/6886EN_04_Code/ch04_src/simplesessionbeanclient/target/simplesessionbeanclient.jar
Jul 06, 2017 12:52:57 PM org.glassfish.apf.impl.DefaultErrorHandler error
SEVERE: Class [ Lnet/ensode/glassfishbook/SimpleSession; ] not found.
Error while loading [ class net.ensode.glassfishbook.SessionBeanClient ]
Exception in thread "main" java.lang.NoClassDefFoundError: net/ensode/glassfishbook/SimpleSession
at net.ensode.glassfishbook.SessionBeanClient.invokeSessionBeanMethods(SessionBeanClient.java:12)
at net.ensode.glassfishbook.SessionBeanClient.main(SessionBeanClient.java:19)
Caused by: java.lang.ClassNotFoundException: net.ensode.glassfishbook.SimpleSession
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at org.glassfish.appclient.client.acc.ACCClassLoader.findClass(ACCClassLoader.java:237)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
UPDATE2: From the Glassfish book:
We are using Maven to build our code. For this example, we used
the Maven Assembly plugin (http://maven.apache.org/plugins/maven-assembly-plugin/) to build a client JAR file
that includes all dependencies; this frees us from having to specify
all the dependent JAR files in the -classpath command-line option
of the appclient utility. To build this JAR file, simply invoke mvn
assembly:assembly from the command line.
SOLUTION: the missing link was producing a client jar with additional jar's "on board" so to speak. Proceed as follows (at least in Eclipse): select pom.xml > right-click > Run As > Maven build... > enter in Goals field: assembly:assembly> Apply/Run.
The result will be that you will find TWO jars under your target folder: xxxclient.jar and xxxclient-jar-with-dependencies.jar.
From the command line in bash execute from the folder with latter jar:
/path_to/appclient -client xxxclient-jar-with-dependencies.jar
After a very long wait (on my $200 mini Linux box) the HelloWorld-ish server EJB gets finally properly called.
Your assumption is right.
You are missing net.ensode.glassfishbook.SimpleSession in your classpath.
From an older book online:
...executed through the appclient utility. This utility can be found at
[glassfish installation directory]/glassfish/bin/. Assuming this path
is in the PATH environment variable, and assuming we placed our client
code in a JAR file called simplesessionbeanclient.jar, we would
execute the above client code by typing the following command in the
command line:
appclient -client simplesessionbeanclient.jar
It seems that you've started from
.../bin/./appclient -client
/home/fedora/Downloads/6886EN_04_Code/ch04_src/simplesessionbeanclient/target/simplesessionbeanclient.jar
You need SimpleSession.class in your CLASSPATH (or in a jar in that classpath).
Usually java checks the current directory first (which is your bin folder). If the class is not found (its not, since its in your simplesessionbeanclient folder), it searches for that class in the classpath (where you did not add the simplesessionbeanclient folder).
Try
appclient -client simplesessionbeanclient.jar
from the folder where simplesessionbeanclient.jar is located.
If you don't want to add the appclient folder to your path start with
/your/path/to/appclient -client simplesessionbeanclient.jar
(again from the folder where simplesessionbeanclient.jar is located)
Update:
If you still get a ClassNotFoundException have a look if it is missing in your jar file (jars are Zip-File, you could use your Zip-Tools):
jar tf simplesessionbeanclient.jar
if there is a SimpleSession.class
I did the following to fix my problem:
Use appclient -classpath (instead of appclient -client)
Use the regular project JARs (instead of the one generated by mvn assembly:assembly)
Deploy the EJB to Glassfish (simplesessionbean.jar)
The example code from a more recent book "Java EE 8 Application Development" by David R. Heffelfinger (the same author of "Java EE 7 for Glassfish") is almost exactly the same (the only minor difference is classes are packaged in "net.ensode.javaee8book" instead of "net.ensode.glassfishbook").
When running appclient.bat -client simplesessionbeanclient-jar-with-dependencies.jar I kept getting:
java.lang.ClassNotFoundException: <mainclass>
errors. This was because the POM was assembling a manifest with <mainClass> value of "net.ensode.glassfishbook.SessionBeanClient" (instead of "net.ensode.javaee8book.SessionBeanClient"). So I decided to avoid using the -client option for appclient.bat and switched to -classpath which allowed me to specify the main class on the command line (which is easier than updating the POM or refactoring the packages to suit the manifest).
But then when running the appclient command:
PS C:\home\programs\java_ee_sdk-8u1\glassfish5\glassfish\bin> .\appclient.bat -classpath "C:\home\code\Java-EE-8-Application-Development-Code-Samples-master\ch04_src\simplesessionbean\target\simplesessionbean.jar;C:\home\code\Java-EE-8-Application-Development-Code-Samples-master\ch04_src\simplesessionbeanclient\target\simplesessionbeanclient.jar" net.ensode.javaee8book.SessionBeanClient
I kept getting:
Root exception is javax.naming.NameNotFoundException: net.ensode.javaee8book.SimpleSession#net.ensode.javaee8book.SimpleSession not found]]
errors. This was solved by deploying the EJB (simplesessionbean.jar) to Glassfish via the Admin Console (this missing step was not mentioned in the book). Running the appclient.bat command then worked.
Screenshot of appclient.bat (takes about 15 seconds to load):
Screenshot of EJB deployment:
Alternatively
You can manually compile the client to include all the dependencies by copying SimpleSession.java and SimpleSessionBean.java from the "simplesessionbean" project to the "simplesessionbeanclient" project (remember to refactor the package statements). This will generate simplesessionbeanclient.jar with the EJBs included (Nb: you still have to deploy the EJBs to the GlassFish server). Also make sure that the <mainClass> element in the POM points to the correct package.
You can now use the -client option:

ClassNotFoundException when using java servlet to save files

I use MyExclipse + JAVA to develop web project. I upload a file using ajaxFileUpload and attempt to save the file in Java Servlet. I use apache tomcat as the server to run my project.
But when I run the project, an exception as follows occurs. I google it, it says the reason may be not importing relevant jars in Java Builder Path. But I have configured it. So, I am confused why.enter image description here
enter image description here

Jar gets blocked after putting all permission attribute in manifest file

I have a jar file and its manifest file looks like this:
Manifest-Version: 1.0 Implementation-Vendor: bothers inc.
Application-Library-Allowable-Codebase: *
Application-Name: brocode
Permissions: all-permissions
Created-By: 1.6.0_45 (Sun Microsystems Inc.)
Name: Swingbro$3.class SHA1-Digest: wmSTk1O6Hh0RG1BgxkB7gkIwy58=
Name: Swingbro$1.class SHA1-Digest: wrMTywvCe1Rphm6H4pb/wJNtR1A=
Name: Swingbro.class SHA1-Digest: 1TJ+45M3kOEHdiHJCfORn89SxZg=
Name: Swingbro$2.class SHA1-Digest: 249ntZ47KPSzzM1PnyKUVlz/Nlo=
This jar runs some .exe file. when it was trying to do so it was throwing an exception of permission attribute is missing in jar. so i added Permissions attribute to all permission but after that the jar execution is being blocked by jws( yes i am using jnlp to execute this jar which will execute an executable ie .exe) the source server is added to the exception list as well and jar is also signed with a code signing cert which has ca installed in java key store and client machine and browser.
am i missing something after adding permission attribute to the manifest file?
When running a Jar through a JNLP the the value of permission attribute in java manifest file should be same as the permission attribute in jnlp file.
JNLP file should be like:
<security>
<all-permissions/>
</security>
Manifest should have:
Permissions: all-permissions

Pentaho PDI Failed to load ESAPI.properties as a classloader resource

I am running a fresh install of Pentaho Data Integration 5.0.1.A Stable from:
http://community.pentaho.com/projects/data-integration/
on my macbook pro, java 1.7.0_25, and I keep seeing this error in the console:
Attempting to load ESAPI.properties via file I/O.
Attempting to load ESAPI.properties as resource file via file I/O.
Not found in 'org.owasp.esapi.resources' directory or file not readable:
/Applications/pdi-ce-5.0.1.A/data-integration/ESAPI.properties
Not found in SystemResource Directory/resourceDirectory: .esapi/ESAPI.properties
What are the ESAPI.properties used for? What should they be set to by default?
thanks, -John
This is a known bug (PDI-10568) that should be fixed in an upcoming release. As a work around, try putting the default ESAPI and validation properties in your $HOME/.esapi/ folder. Create one if it doesn't already exist.
Background: ESAPI is an Enterprise Level Security library used by Pentaho webservices to properly encode URLs and HTML content, read more at https://www.owasp.org/index.php/ESAPI