Trouble Creating jarfile

I've had little success with creating my own jarfiles. Here's my latest attempt. I have the java file hprsPurchase.java:

import java.util.Date;
import java.text.SimpleDateFormat;

class hprsPurchase {

public static Date snagFinalPaymentDate()
{
    return new Date();
}

} // endclass

Open in new window



I do the following:

$ javac hprsPurchase.java
$ jar cf hprsPurchase.jar hprsPurchase.class
$ sudo cp hprsPurchase.jar $CATALINA_HOME/common/lib

Open in new window


I add $CATALINA_HOME/common/lib/hprsPurchase.jar to my CLASSPATH and restart tomcat

I add <%@ page import="hprsPurchase.*" %> to my jsp file. Without referencing the snagFinalPaymentDate() function the jsp file "compiles" at this point. (If I leave off the ".*" I get: The import hprsPurchase cannot be resolved")

When I do reference the function:

Date now = new hprsPurchase.snagFinalPaymentDate();

Open in new window


I get the error:

An error occurred at line: 192 in the jsp file: /finalPaymentReport.jsp
hprsPurchase cannot be resolved to a type;

I also tried:

Date now = new Date();
now = hprsPurchase.snagFinalPaymentDate();

Open in new window


and got:

hprsPurchase cannot be resolved

What am I doing wrong?

Linux kernel 2.6.37.6
javac version: 1.7.0_02
tomcat version: 6.0.14
LVL 1
jmarkfoleyAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
$CATALINA_HOME/common/lib.

That should really be in WEB-INF/lib
0
 
jmarkfoleyAuthor Commented:
thanks!
0
 
CEHJCommented:
I would not use jars in that context without using an explicit package

package hprsPurchase;

import java.text.SimpleDateFormat;

import java.util.Date;


public class HprsPurchase {
    public static Date snagFinalPaymentDate() {
        return new Date();
    }
} // endclass

Open in new window


The class must be public. Class names in Java begin upper case
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
jmarkfoleyAuthor Commented:
I've made your suggested changes. I now get:

An error occurred at line: 21 in the generated java file
The import HprsPurchase cannot be resolved

I tried both of:

<%@ page import="HprsPurchase" %>
<%@ page import="HprsPurchase.*" %>

I've renamed the program to HprsPurchase.java. It is:
package hprsPurchase;

import java.util.Date;
import java.text.SimpleDateFormat;

public class HprsPurchase {

public static Date snagFinalPaymentDate()
{
    return new Date();
}

} // endclass

Open in new window

I did:
$ javac HprsPurchase.java
$ jar cf HprsPurcahse.jar HprsPurchase.class
$ sudo cp HprsPurchase.jar $CATALINA_HOME/common/lib

Open in new window


I modified the CLASSPATH for the new jarfile and restarted tomcat.

You know, I've created function libraries on numerous platforms and this jarfile thing keeps stumping me!
0
 
CEHJCommented:
<%@ page import="hprsPurchase.HprsPurchase"%>

Open in new window

is what you need
0
 
jmarkfoleyAuthor Commented:
An error occurred at line: 21 in the generated java file
Only a type can be imported. hprsPurchase.HprsPurchase resolves to a package
0
 
CEHJCommented:
hprsPurchase.HprsPurchase resolves to a package 

Open in new window

No - it resolves to a type (as long as you used the code i posted)
0
 
jmarkfoleyAuthor Commented:
Hmmm, I must not have posted my reply ... try again ...

I believe I have exactly the code you suggested. See my code in post 39751597, above. Is that not exactly what you have? So, why do I get the "hprsPurchase.HprsPurchase resolves to a package" error? ( you see, this jarfile stuff is *not* very straight-forward!)
0
 
CEHJCommented:
Can you please post the result of the following command?


jar tf HprsPurchase.jar 

Open in new window

0
 
jmarkfoleyAuthor Commented:
$ jar tf /srv/tomcat/common/lib/HprsPurchase.jar
META-INF/
META-INF/MANIFEST.MF
HprsPurchase.class
0
 
CEHJCommented:
That's wrong. You need to jar from above the package directory (hprsPurchase)
0
 
jmarkfoleyAuthor Commented:
Don't understand. Please explain further
0
 
CEHJCommented:
hprsPurchase/HprsPurchase.class 

Open in new window

is what the listing should say

So your command should be

jar cf hprsPurchase.jar hprsPurchase

Open in new window

and of course HprsPurchase.class should be in directory hprsPurchase as a result of compilation
0
 
jmarkfoleyAuthor Commented:
OK, didn't know about the directory structure thing ...

Stiil no joy :(
org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 21 in the generated java file
Only a type can be imported. hprsPurchase.HprsPurchase resolves to a package

An error occurred at line: 192 in the jsp file: /finalPaymentReport.jsp
HprsPurchase cannot be resolved

Open in new window

The jar now contains:

$ jar tf /srv/tomcat/common/lib/hprsPurchase.jar
META-INF/
META-INF/MANIFEST.MF
hprsPurchase/
hprsPurchase/HprsPurchase.class

Relavant jsp lines:
<%@ page import="hprsPurchase.HprsPurchase" %>
Date now = HprsPurchase.snagFinalPaymentDate();

Open in new window


If I change to <%@ page import="hprsPurchase.HprsPurchase.*" %> // wildcard
the import error goes away but I still get:

An error occurred at line: 192 in the jsp file: /finalPaymentReport.jsp
HprsPurchase cannot be resolved
0
 
CEHJCommented:
If I change to <%@ page import="hprsPurchase.HprsPurchase.*" %> // wildcard
the import error goes away

That doesn't make sense, but you could of course do
<%@ page import="hprsPurchase.*" %>

Open in new window

0
 
jmarkfoleyAuthor Commented:
Even when I do that [wildcard] I still get the error:

An error occurred at line: 192 in the jsp file: /finalPaymentReport.jsp
HprsPurchase cannot be resolved

So, still not working. Any ideas?
0
 
CEHJCommented:
I think you should perhaps post a link to the jar file itself so i can take a look at it

Alternatively you can use THIS
0
 
jmarkfoleyAuthor Commented:
Got the same error using your jarfile. I had to add the wildcard to eliminate the import error:

An error occurred at line: 192 in the jsp file: /finalPaymentReport.jsp
HprsPurchase cannot be resolved

I've posted my jarfile and source code: http://www.novatec-inc.com:/pub/hprsPurchase.zip
0
 
jmarkfoleyAuthor Commented:
And here is a stripped down version of the .jsp program:

<%@ page import="java.util.Date" %>
<%@ page import="HprsPurchase.HprsPurchase.*" %>
<html>
<body>
<%
Date now = HprsPurchase.snagFinalPaymentDate();
%>
</body>
</html>

Open in new window


Can you get this and the jarfile working together?
0
 
CEHJCommented:
In which directory did you put your jar?
0
 
jmarkfoleyAuthor Commented:
$CATALINA_HOME/common/lib. That's where all the 3rd party jarfiles are:

$ cd $CATALINA_HOME/common/lib
1 01:20:35 root@webserver:/srv/tomcat/common/lib
$ ls -ltr
total 1192
-rw-r-xr-x 1 root root 253122 2007-09-29 17:10 sqljdbc.jar*
-rw-r-xr-x 1 root root  57779 2009-01-09 12:16 commons-fileupload-1.2.1.jar*
-rw-r-xr-x 1 root root 109043 2009-01-10 09:01 commons-io-1.4.jar*
-rw-r-xr-x 1 root root 466359 2009-07-14 06:36 sqljdbc4.jar*
-rwxr-xr-x 1 root root  14146 2013-09-30 12:56 opencsv-2.3.jar*
-rwxr-xr-x 1 root root 284220 2013-12-19 04:12 commons-lang-2.6.jar*
-rwxr-xr-x 1 root root    833 2014-01-04 12:37 HprsPurchase.jar*

Open in new window


I may have rather stupidly put the wrong jarfile in the zipfile link I sent you. The one that is the same as in the above ls is http://www.novatec-inc.com/pub/HprsPurchase.jar

The java source code in the zipfile is the current version.
0
 
jmarkfoleyAuthor Commented:
This jarfile (if I ever succeed in creating it) is not specific to one webapp. I've got several webapps, plus numerous regular java batch programs that I'd like to use this eventual jarfile. All the other 3rd party jarfiles reside in $CATALINA_HOME/common/lib, so why not one I create? I would assume this is possible, right?

Otherwise, do you have any other ideas or are you stumped too?
0
 
CEHJCommented:
Oh OK. That's different, so yes it can stay there.

I don't see what's wrong now. Try deleting the compiled jsps and restarting the container
0
 
jmarkfoleyAuthor Commented:
Deleted the finalPaymentReport_jsp.java file, restarted tomcat. Same error:

An error occurred at line: 6 in the jsp file: /finalPaymentReport.jsp
HprsPurchase cannot be resolved

Do you have linux/tomcat? If so, on your end, try putting my one-line snagFinalPaymentDate() function (post ID 39751144 into a jarfile and referencing it from my one-line jsp (post ID: 39756456) and see if you can get it to work. I don't think we can get much simpler than that. If you get it working, post the step-by-step for me. If it works for you but doesn't work for me then there must be something awry with my java/tomcat setup. Otherwise, I think we're just guessing at this point.
0
 
CEHJCommented:
<%@ page import="java.util.Date" %>
<%@ page import="HprsPurchase.HprsPurchase.*" %>
<html>
<body>
<%
Date now = HprsPurchase.snagFinalPaymentDate();
%>
</body>
</html>
                                            

Open in new window

is of course wrong. It should be
<%@ page import="hprsPurchase.*" %>

Open in new window

0
 
jmarkfoleyAuthor Commented:
Huh, for some reason some of my posts are not getting posted. Maybe I'm fogetting to 'post' after reviewing. To repeat my post from yesterday:

I think you are right about the jar needing to be in WEB-INF/lib. I made a symlink from there to $CATALINA_HOME/common/lib/HprsPurchase.jar and got it working. The jarfile now has:
$ jar tvf /srv/tomcat/common/lib/HprsPurchase.jar
     0 Sat Jan 04 14:26:18 EST 2014 META-INF/
    68 Sat Jan 04 14:26:18 EST 2014 META-INF/MANIFEST.MF
     0 Sat Jan 04 14:24:54 EST 2014 hprsPurchase/
   326 Sat Jan 04 14:24:54 EST 2014 hprsPurchase/HprsPurchase.class

Open in new window


and my jsp file is:
<%@ page import="java.util.Date" %>
<%@ page import="hprsPurchase.*" %>
<html>
<body>
<%
Date now = HprsPurchase.snagFinalPaymentDate();
out.println(now);
%>
</body>
</html>

Open in new window


That worked. This jarfile now also works with a java program:
import hprsPurchase.*;
:
Date now = HprsPurchase.snagFinalPaymentDate();
System.out.println(now);

Open in new window

but, I have to make sure the HprsPurchase.[java | class | jar ] files are *not* in the same directory as my java program. javac will pick them up and generate errors.

So, I think the initial problem is solved. I will post another question later about why $CATALINA_HOME/comm/lib is not seen by tomcat, but that's a separate issue.
0
 
jmarkfoleyAuthor Commented:
Thanks for working this through with me. That was painful!
0
 
CEHJCommented:
I think you are right about the jar needing to be in WEB-INF/lib. I made a symlink from there
Making a symlink could be dodgy. It could cause classloading problems
0
 
jmarkfoleyAuthor Commented:
I'll post a separate question about tomcat and where it looks for jars. I don't want to have to have a copy of the same jarfile in all webapps' WEB-INF/lib folders AND a copy somewhere for java program.
0
 
CEHJCommented:
No - it's ok to have it in Tomcat common. The bad bit is the symlink
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.