I have an extremely simple web app for experimenting in java. I have the following java class defined:
UtilDate.java http://www.philbair.org/samples/UtilDate_java.jpg
And a JSP file as follows:
UtilDate.java http://www.philbair.org/samples/getdate_jsp.jpg
The tomcat folders/files look like this (this is not official syntax, just an abstract representation of the folder structure):
UtilDate.java http://www.philbair.org/samples/tree.jpg
mywebapp/WEB-INF/classes/com/udate/UtilDate.class
The jsp file is under the root 'mywebapp' folder (name is inconsequential), and all these are under the webapps folder in tomcat.
After compiling this java class (UtilDate.java), I restart tomcat to make sure it's not hanging on to any old cache stuff.
I get the following...
javax.servlet.ServletException: java.lang.NoClassDefFoundError: UtilDate
when I try to put the jsp page in the browser.
If I replace the uDate() method in the print statement of the jsp file with
out.print("hello");
I get the word hello on the browser output.
This is crude code for testing a simple web app. I am not looking for advice on best practices (although that advice is excellent from what I've seen on this site), I just want to know why this doesn't work. It worked once, and after recompiling the class, it stopped working. I can run the class on my command line and it works just fine (outputs the date as in the main method).
I'm running Windows 7, and tomcat 7.0.34. I'm not using eclipse, or netbeans, etc. at the moment. I'm using a simple text editor, compiling the java class with the command line javac, creating the web folders manually, and placing all the files where they belong myself instead of having maven or ant or anything else build it for me. No war file at the moment. Just working with the files right in the tomcat folders under webapps. I know there are more sophisticated and recommended ways of doing this, but it's only a test for my own understanding with nothing to shield me from what's going on.
Again, this worked once.
Thanks in advance for any help.
you forgot to add the statement package com.udate; at the top of your java class. Add and compile , you should be through
Related
I have a working Spring 5 application which works great. BTW, it is Spring 5 CLASSIC, and does NOT use Spring Boot. So, it is not using an embedded Tomcat. I built a traditional WAR file, and deployed it to an existing Tomcat installation.
This project is a multi-maven module project that is made up of several jars, and ending in one WAR file. This runs great. What I am trying to do is execute one class in one of these jars as a standalone piece of code, it's a utility which I want to run nightly. I know it runs in Eclipse just fine, so I know the code is ok.
Based on the readings here, it is very obvious what has to be done.
java -cp SomeJava.jar com.tomholmes.products.myproject.server.util.MyClassUtil
Yes, this 'MyClassUtil' does indeed have a "main" method as you would expect.
public static void main(String[] args) throws Exception {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
MyClassUtil myClassUtil = new MyClassUtil();
myClassUtil.processBackLog();
}
I have deployed a the WAR file (MyProject.war) is deployed on my linux box, and tomcat is located at: /opt/tomcat the war file is deployed to: /opt/tomcat/webapps
and when we restart tomcat, we have:
/opt/tomcat/webapps/MyProject
/opt/tomcat/webapps/MyProject/META-INF
/opt/tomcat/webapps/MyProject/WEB-INF
/opt/tomcat/webapps/MyProject/WEB-INF/classes <--- no files at all
/opt/tomcat/webapps/MyProject/WEB-INF/libs <--- contains all JAR files including
the one that has my class in it
/opt/tomcat/webapps/MyProject/WEB-INF/libs/my-utils.jar <--- contains the class I want to
execute
I have a script file 'my_util.sh' that looks like this.
export JAVA_ROOT=/opt/java
export JAVA_HOME=/opt/java
export JAVA_BINDIR=/opt/java/bin
export JRE_HOME=/opt/java/jre
export JRE_BINDIR=/opt/java/jre/bin
export PATH=${JAVA_HOME}:${JAVA_BINDIR}:$PATH
export PATH=${JRE_HOME}:${JRE_BINDIR}:$PATH
export PATH=${JAVA_HOME}:${JAVA_BINDIR}:$PATH
datestr=$(date +%Y%m%d_%H%M%S)
export CLASSPATH=/opt/tomcat/webapps/MyProject/WEB-INF/libs/my-utils.jar
export MAINCLASS=com.tomholmes.products.myproject.server.util.MyClassUtil
java -cp ${CLASSPATH} ${MAINCLASS}
To which I get the response:
Error: Could not find or load main class com.tomholmes.products.myproject.server.util.MyClassUtil
Based on the past messages here, of which there are many, and I did research, I should have had this resolved by now. And the way I setup everything should be working, but it is not. I am sure I am missing something small, and I feel like I am almost there. If anyone can make a suggestion or help me out with this, that would be great. I'll keep looking for the answer myself to see if I can get it before this question gets answered.
Thanks!
I just figured this out. As I said, the code itself works great from within Eclipse and is unit tested. When you run a class within Eclipse, you can run a Java Class as a standard Java application, and it will create a Configuration for you with a setup classpath. There is even a button that says "Show Command Line" which showed the full classpath, call to java, any -D arguments, and the actual class.
So, there were two things that I missed:
1) I needed to include ALL the jars that were associated with this class. I know that was mentioned in the comments, and I had tried that because I suspected it was the case.
2) However, what I messed up on was the separator between jars. I tried comma (,) and semi-colon (;) before figuring out that I needed the colon (:). Looking the command-line that Eclipse put together clued me in on that.
It was asked, did I try to execute this on the command-line before I did the script, and the answer is: I did not. I just thought I'd go to the script directly.
BTW ... I figured that having some standalone utilities with my overall web-application wasn't a huge problem. It wasn't worth it to create a whole new project for one utility.
Now that I know this script runs, now I can put it on a cron job.
Thanks for the help. And I hope this helps someone else.
I have an application (DrawFBP) which has a Help facility. built using JavaHelp. I actually had it working for several years on the Eclipse IDE, but I had never tested it running in the jar file. The Help facility uses a number of html files, and some other stuff, and one informant said I can get them included on the jar file by putting them in a resources folder (src/main/resources) . This seems to have worked! After lots of restructuring, I got my Help facility working on the jar file, but it stopped working on the IDE. The IDE version is crashing on a FileNotFoundException referencing a file with extra levels of qualification - where do they come from? I am probably making several dumb errors - but help would be much appreciated! The project can be found on GitHub - https://github.com/jpaulm/drawfbp . The code and resources are there, but Git wants me to add all my code back in again - not sure why... TIA
Somehow got it working - the trick seems to be to include the resources in a folder called 'resources' under src/main. I will accept this answer - unless someone comes up with a better one!
I have an Eclipse Java project, in which I have an src/demo package that contains a Processing demo, inside a single class that extends PApplet. The demo runs fine in Eclipse.
I wish to demo this package, preferably in a web browser. Being able to demo outside the Eclipse project would still be appreciated.
I've tried exporting the package to a JAR and embedding it in an HTML file. Firefox seems to be the only modern mainstream browser that supports NPAPI plugins like Java (Chrome 46 just shows a grey box, Safari 9 shows a blank page).
Firefox kept throwing "Class/Library not found" errors, until I manually copied all the libraries to the same directory as a JAR and including them in archive attribute of the applet element in the HTML.
Now Firefox shows a black outline but a blank app, which might because I haven't copied assets from the data folder in Eclipse. I have no idea how to include them with the JAR however.
I tried following this guide and simply adding a main() to the PApplet but that gave me a "Class not found error" when I exported it as a runnable JAR and tried running it.
System information: Mac OS X 10.11.0, Java 1.8.60
What is the systematic way to go about all this?
I think the best way to answer your question is to clear up some of the confusions you have about the issues you're experiencing.
First of all, applets are pretty much dead. They won't work at all in chrome, like you mentioned. Getting them to work in other browsers is a pain in the neck, for you and for your end users. You have to either pay for a certificate or get your users to change their Java security settings.
Furthermore, as of Processing 3, PApplet no longer extends Applet. So you can't simply extend PApplet and treat it as an applet anymore. (You forgot to tell us which version of Processing you're using.)
Honestly, the best approach is to forget about applets.
If you forget about applets, then you could deploy as a standard executable by going to file -> export, or as a runnable jar using something like JarMatey (full disclosure: I wrote JarMatey). It's important to note that it's not enough to simply add a main() function to your class.
But in my honest opinion, your best bet for deploying your Processing sketch is to use Processing.js to deploy as html5/JavaScript. This should be pretty easy, as long as you aren't using any Java libraries.
I'm working on a Swing application that has to be localized.
For that purpose, I'm using a resourceBundle. It works fine when I run the application from the IDE. But when I use maven to generate the jar, the application starts up normally but the internationalization stops working. Without throwing any exception, I have make a lot of variations to solve that problem; I even have make a class that inherits from java.util.ResourceBundle.Control. The only thing I've achieved to make it work is by placing the properties files outside the package (I place it into the desktop just for testing). I then try to use the same method by specifying the path by new File(".").getAbsolutePath().
Doing that still keeps working from running it from Eclipse, but when I try to generate the jar and execute it, it throws an exception and the application does not start.
If you place your properties file into src/main/java next to the Java code, Maven will probably not pick it up.
The canonical location would be src/main/resources (with the package structure below that duplicated just as it is now in your code).
I'm using Tomcat 7.0 with Eclipse. The tomcat server is synchronized with eclipse. After creating my first class, I put the .java file under src/(default package)/HelloWorld.java (not good practice I know but just for testing)
The content is just as follows, fairly simple:
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
#WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println ("Hello World");
}
}
Well many tutorials claim that I must use javac to compile the code. But I did nothing and it ran with no problem. Also everytime I change the code it updates immediately just like magic. Something must be working but I don't know what it is.
Yeah it's obviously a newbie question so any help is welcome. Also It's better if you have any systematical and easy-to-follow tutorial links. I'm searching for them for several days but got lots of inconsistent answers.
To me, You mix 2 main technologies - tomcat as a web container and eclipse as your IDE. Their integration confuses you.
Lets leave JSP for now and talk only about servlets, because it confuses even more
Tomcat cannot work with source files (*.java). You Must compile your application with javac for example and create something called WAR - web archive - a zip file that will contain your compiled class and adheres some EE standards that tomcat understands (its also possible to use folder instead of zip, but lets put it aside as well, its not relevant for this explanation).
Among others this war (once compiled correctly) will contain your compiled servlet class HelloWorld.class).
Once tomcat is started and recognizes your war file in deployment folder it opens it and loads in runtime.
No compilation, only runtime loading.
Now people talk here about JSP.
In fact JSP is something that is technically equivalent to servlet but resembles HTML.
You put the file with extension .jsp and build your WAR. java compiler can't read JSP files, so you should put them into your war file somehow (usually build tools/IDE do it for you). Bottom line is you have JSP files as you've created them in your war.
Now you put your war into Tomcat, it recognizes it as before and loads. At this point it still does nothing with your JSPs.
So, your war is deployed, tomcat is started and go to 'http://localhost:8080/myfirstjsp.jsp' from your browser
At this point (first invocation of your JSP) a lot of thing happen:
Tomcat gets your browser's HTTP request
Tomcat recognizes that it should process the JSP file
Tomcat parses your JSP file
Tomcat compiles it internally to some class file that you're not aware of (its stored internally in Tomcat),
Tomcat loads this file in runtime and treats it as a compiled Java class.
Next time you'll invoke the JSP, it will be already compiled.
The last question here is how Eclipse connected to all this story :)
In fact Eclipse has an integration with tomcat, so all the war-creating-and-deploying stuff is transparent to do. That's why you push 'play' on eclipse and it compiles your project, creates a war, makes sure tomcat is aware of this war (configures deployment related stuff), starts tomcat and voila! - you have you application working.
Its important to understand what happens at what level
Hope this helps
Mark
It looks like you are using the tomcat server plugin with Eclipse. In this case, as soon as you save your .java file, eclipse compiles it and updates the class files in tomcat server automatically.
Eclipse is an IDE, it does most of the things for you automatically like compiling the code, setting up classpath to include the required jar files etc.
If you want to follow the tutorials, I would suggest you use a plain text editor and a standalone tomcat server. Then, you will have to do all the steps mentioned in the tutorial(compiling the servlet class using javac, copying the .class file to tomcat server etc.)
No. Java EE container (Tomcat) cannot compile .java files automatically/implicitly. In fact the JSP engine of container will parse the JSP and generate the class file - JSP-wiki
Take a look at Eclipse Build Story.