I am using Eclipse Helios to program JSP and POJOs running on Tomcat 5.5. I am trying to use the org.apache.catalina.connector.Response class as follows:
import org.apache.catalina.connector.*;
...
Response resp = (Response) r;
where r is an instance of HttpServletResponse. Eclipse gives no warnings, and the project compiles fine. However, when I browse to a page that contains this code, I get the following error:
java.lang.ClassNotFoundException: org.apache.catalina.connector.Response
any idea of what is going wrong?
I have also tried it this way, with the same result.
org.apache.catalina.connector.Response resp = (org.apache.catalina.connector.Response) r;
Thanks,
Ean
A ClassNotFoundException means that a class was available at compile time, but not at run time. So it makes no difference how you import it in your source code.
I haven't checked, but it's quite possible that Tomcat doesn't let web apps access its internal classes. And the reason I haven't checked is that I know it'd be a bad idea even if I could do it. You should code to the API (in this case, the stuff under javax.servlet.*) not to the implementation.
Clearly, the Tomcat designers don't think that it is a good idea for webapps get at its internal classes. Here's a couple of possible reasons:
It makes your code dependent on Tomcat.
It makes your code fragile in the face of changes to internal details; i.e. it depends on the Tomcat version.
The Tomcat stack or other things inside your webapp context could do things that mean that your Response object is a wrapper rather than the class you are expecting.
To change make your code "work" you would probably need to tinker with the Tomcat JAR files ... which will make it even less portable. For more information that might help you to slit your own throat, refer here. Note that this is Tomcat 5.5 specific ... and the Tomcat 6.0 version of the page is different.
The reason I want to turn it to a org.apache.catalina.connector.Response is to read what's in the HTTP response headers before they are sent to the client.
I see. Well perhaps you should be implementing a custom Valve. (This is a Tomcat-specific thing.) Or better still, see if one of the existing Valve implementations will do the job for you.
I don't' think you can use that class. I am not sure what you are trying to do, but use HttpServletResponse and you should be good to go.
Related
Some Confusion about the source code of Tomcat
Some day before, I began to read the source code of the tomcat
First, the server starts from the main method in the org.apache.catalina.startup.BootStrap, but when I go into the code bootstrap.init(),
and I was confused by the following code.Like:
Just for easy to debug the tomcat, I change the code like this:
The code can still work. And in the Tomcat source code, there are many code block like this, For example,in org.apache.catalina.startup.BootStrap.start()
we can find the following code:
Still for easy to debug, I turn the code into:
>
The code still works fine.So I was confused, Here are my questions.
what's difference between the two kinds of code?
Why the coder of Tomcat do not write the code like what I write? what's the benefit?
If the Tomcat with my kind code works in a production environment, what will happen or nothing will happen?
1) what's difference between the two kinds of code?
The Tomcat code eliminates a static dependency on the Catalina class. In general, that has a couple of potential benefits:
It may allow you to substitute an alternative version of that class ... without changing the code. (But not here, because the FQN for the class is hard-wired.)
For some JVMs, it may result in deferred class loading which can give faster JVM startup. (I'm not sure if that is relevant here.)
In addition, the Tomcat code is explicitly creating a new classloader and loading the Catalina using that ... rather than the default one for the Bootstrap class. I'm not sure of the significance of that.
2) Why the coder of Tomcat do not write the code like what I write? what's the benefit?
Ask the coder. The (possible) benefits are as I described above, but it may have been done for some other reason ...
3) If the Tomcat with my kind code works in a production environment, what will happen or nothing will happen?
Probably nothing, though it might affect the classloader organization for Tomcat in some way that impacts on your webapps.
But if you are worried, don't change the code! There is no real need to change this ... based on why you said you are doing it. There's a saying:
"If it ain't broken, don't fix it."
Well, at least it seems as tapestry issue.
I'm getting an exception when trying to access a specific page:
org.apache.tapestry.PageNotFoundException:
Page 'ASpecificPage' not found in application namespace.
Seems like some pages are found, and some are not, I'm new to this project, and so far cannot say is there any difference between them.
The error still occurs, even when whole jboss-4 instance, along with the deployed application, is copied from my coworkers' computers and run, whereas they do not experience such problems. Supposedly there is no other application-specific configuration, and it's all contained within the server instance.
Any ideas? Even not Tapestry-related? I tried using same java version, with update number precision, but in vain. Like, where should I start to debug?
EDIT:
I'm new to tapestry, so if anything is missing or just inaccurate, just let me know. Some details:
the application is deployed as war, copied to deploy directory,
unexploded
there are other jars copied as well, not sure if they are relevant
when searching for ASpecificPage, it seems that there is only a java
class (i.e. ASpecificPage.java), and there is missing appropriate
.html file
when searched in code, I found occurences like:
clearPopUp(cycle);
ASpecificPage page=(ASpecificPage) cycle.getPage("ASpecificPage");
page.show(cycle, ASpecificPage.TAB_2);
Any other details needed?
before asking, please understand that my english is not good.
I'm using Class.forName(...) class in a servlet programming. when I access the servlet, I get a row of detailed controller information from Database indicating which controller to use.
This is Class.forName(...) I coded:
Class c = Class.forName(row.getControllerInfo);
c.newInstance();
This works fine, but there's a problem, i'm using Eclipse. The problem is that when I modified the Controller file, the changed contents were not applied to the server.,,.
Probably the easiest way is not to support dynamic loading. Much better to achieve something like dynamic update by supporting multiple servers. For development, you could get around redeploy delays by using JRebel (there might be others).
If you really do want dynamic loading of classes then the answer is "class loaders". I suggest having a look at those, and come back with any specific questions.
If I understood your problem true,
When you change any file of your project, you must deploy your project to server. If you use server from eclipse, republish may solve your problem.
have you tried clean - re-built and then deploying your application?
I have a jar package that I wrote using netbeans. This package is called from other java file. The jar calls a webservice and is supposed to do something with it. Now everything works fine locally. I compiled the files and locally and uploaded them to the server and when I run it, I get the "Service could not be initialized".I am not sure how to debug this. I am pretty new to java. What is the best approach here to solve the issue?
I would start by implementing logging (I like log4J) in your project so you can get some better details of what is actually going wrong. This will be very useful not only now but in the future as things go wrong (they inevitably will) you will be able to solve them based on how good of a job you did logging what is happening in your application. Right now it sounds like an error is bubbling up and you're not getting much detail about it. Logging should help you determine not only what went wrong but where it happened and what the application was doing at the time.
Try this short introduction to log4j to get started.
I have a rather complex J2EE app I don't have any documentation for and I am trying to get it to run.
I have gotten the ant build script to compile a EAR file that contains a WAR file, but this application even though I get "successfully deployed" on weblogic console is still not working.
There are many required jars missing from the EAR file, hence I get errors about missing classes on the console log when I deploy the app. Sometimes even after I check a particular class is there I still get the error.
What is a best way to debug and get this application running?
Is there any shortcuts in J2EE/Java to "find all dependencies", apps anybody knows to analyze code and find dependencies or anything like that?
Is there any shortcuts in J2EE/Java to "find all dependencies", apps anybody knows to analyze code and find dependencies or anything like that?
Do you mean a compiler? I'm actually semi serious here (even if the compiler won't give you the name of a missing JAR). Indeed, if you are compiling that application successfully with Ant, then you likely have all dependencies required at compile time (you may need more of them at runtime but, well, you'll need to execute the code to identify them). Maybe you just need to add more of them in the EAR during the packaging. Or maybe you need to add more dependencies at the app server classpath level.
In both case, search engines like jarFinder.com or Docjar.com or Jarhoo.com might help you to identify missing JARs and to solve your ClassNotFoundException or NoClassDefFoundError.
You should actually give readers more details about the missing classes, I'm sure people will be able to give you some hints and point you in the right direction.
I know of no easy way to deal with this. If the application provides an ant task build the EAR then the result should be a self-contained deployable application EAR. It is possible that the EAR requires extra libraries to be added to some class path, without documentation it's really hard to know what.
This phrase is, I guess, at the heart of the problem: "Sometimes even after I check a particular class is there I still get the error." Exactly what do you mean?
You are getting a class not found error but you can see the class in the EAR file? If that's the case then things are really difficult, and may well be some kind of classloader issue. I don't know WebLogic at all, but in WebSphere when deploying an application you have a choice of whether to give precedence to JARs in the EAR or to the same JAR in WebSphere itself. Some Applications demand one or the other setting. If there's anything like that in WebLogic then this may be your problem.
Another possible problem is that the app may depend on infrastructure libraries (Eg. XML parsers) that are supplied by WebLogic, but expects a different version that is supplied with the WebLogic version you are using.
Without documentation that the app is supported on the version of WebLogic you are using, I fear you're fighting a long, hard (or even a losing) battle.
I'd assume you managed to get this application put together and compiled in an IDE, maybe Eclipse. Why not simply put all the Jars that you had available at build time, into the EAR?
If you're having to debug this by trial and error, you can look at the list of missing classes and figure out which jars they belong to by Googling for the class names, or maybe you can find them in your IDE too.
If you have a lot of trouble identifying some classes, post the class names (or the error messages) here and someone should be able to tell you where to find the Jar it belongs in.
If you are able to successfully build and deploy the app, but still getting the errors about missing classes, probably some of the jar classes are called through reflection. You may want to search for reflection API calls in the Project through your IDE.
These calls may be something like,
"Class cls = Class.forName(..); cls.newInstance()."
Sometimes even after I check a
particular class is there I still get
the error.
This may be a deployment issue. check the classpath. Some of the jars may be missing from the Classpath.
Is there any shortcuts in J2EE/Java to
"find all dependencies", apps anybody
knows to analyze code and find
dependencies or anything like that?
I'd start by having another go at getting documentation ... or help ... from the original developers, if you can find them. [IMO, people who develop / provide software without any documentation deserve to be bugged incessantly by people asking silly questions.] But I guess you've already tried that.
Then there are the jarfinder.com and other services as mentioned in another answer. (New to me!)
If that fails, I'd try doing a Google search on the FQNs. The chances are that if the missing classes are part of a commonly used library you will hit the Javadocs ... or a posting from someone else with a similar problem to yours.
If the "Google it" approach fails, use the clues in the package naming for the missing classes to try and find where they come from. If they follow the Sun recommendations, the names should map to a company or (real or pseudo-) organization that you can locate by a web search.