Solved

Trouble Creating jarfile

Posted on 2014-01-01
31
296 Views
Last Modified: 2014-01-07
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
0
Comment
Question by:jmarkfoley
  • 16
  • 14
31 Comments
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
<%@ page import="hprsPurchase.HprsPurchase"%>

Open in new window

is what you need
0
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
An error occurred at line: 21 in the generated java file
Only a type can be imported. hprsPurchase.HprsPurchase resolves to a package
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Can you please post the result of the following command?


jar tf HprsPurchase.jar 

Open in new window

0
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
$ jar tf /srv/tomcat/common/lib/HprsPurchase.jar
META-INF/
META-INF/MANIFEST.MF
HprsPurchase.class
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
That's wrong. You need to jar from above the package directory (hprsPurchase)
0
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
Don't understand. Please explain further
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
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
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
In which directory did you put your jar?
0
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
$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
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
$CATALINA_HOME/common/lib.

That should really be in WEB-INF/lib
0
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
<%@ 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
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
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
 
LVL 1

Author Closing Comment

by:jmarkfoley
Comment Utility
Thanks for working this through with me. That was painful!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
No - it's ok to have it in Tomcat common. The bad bit is the symlink
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now