Avatar of Mark
Mark
 asked on

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
JSPJava

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
Mark

ASKER
thanks!
CEHJ

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
Mark

ASKER
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!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
CEHJ

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

Open in new window

is what you need
Mark

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

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)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Mark

ASKER
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!)
CEHJ

Can you please post the result of the following command?


jar tf HprsPurchase.jar 

Open in new window

Mark

ASKER
$ jar tf /srv/tomcat/common/lib/HprsPurchase.jar
META-INF/
META-INF/MANIFEST.MF
HprsPurchase.class
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
CEHJ

That's wrong. You need to jar from above the package directory (hprsPurchase)
Mark

ASKER
Don't understand. Please explain further
CEHJ

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Mark

ASKER
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
CEHJ

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

Mark

ASKER
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?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
CEHJ

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
Mark

ASKER
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
Mark

ASKER
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?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CEHJ

In which directory did you put your jar?
Mark

ASKER
$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.
ASKER CERTIFIED SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mark

ASKER
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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
CEHJ

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
Mark

ASKER
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.
CEHJ

<%@ 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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Mark

ASKER
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.
Mark

ASKER
Thanks for working this through with me. That was painful!
CEHJ

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Mark

ASKER
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.
CEHJ

No - it's ok to have it in Tomcat common. The bad bit is the symlink