Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trouble Creating jarfile

Posted on 2014-01-01
31
Medium Priority
?
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 16
  • 14
31 Comments
 
LVL 1

Author Comment

by:jmarkfoley
ID: 39750773
thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39751144
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
ID: 39751597
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 39751731
<%@ page import="hprsPurchase.HprsPurchase"%>

Open in new window

is what you need
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 39751852
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
ID: 39751911
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
ID: 39753055
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
ID: 39753320
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
ID: 39754128
$ jar tf /srv/tomcat/common/lib/HprsPurchase.jar
META-INF/
META-INF/MANIFEST.MF
HprsPurchase.class
0
 
LVL 86

Expert Comment

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

Author Comment

by:jmarkfoley
ID: 39754488
Don't understand. Please explain further
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39754510
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
ID: 39755476
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
ID: 39755927
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
ID: 39756076
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 39756082
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
ID: 39756296
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
ID: 39756456
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
ID: 39756518
In which directory did you put your jar?
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 39757007
$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 2000 total points
ID: 39757244
$CATALINA_HOME/common/lib.

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

Author Comment

by:jmarkfoley
ID: 39757406
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
ID: 39757484
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
ID: 39758344
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
ID: 39758915
<%@ 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
ID: 39759826
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
ID: 39759829
Thanks for working this through with me. That was painful!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39759971
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
ID: 39761094
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
ID: 39761528
No - it's ok to have it in Tomcat common. The bad bit is the symlink
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

670 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