Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to compile .java files in tomcat server.I researched the net but couldn't find the required answer,if yes then please elaborate how!!
PS: I am new to development...so apologies if this ones a stupid question :)
NOT POSSIBLE
Tomcat expects .class compiled java files. but still you can compile the java file to generate .class file and replace the on under classes folder in the tomcat and redoploy it.
From Does Tomcat automatically compile java servlets?
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 compiler 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 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 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 in browserr you go to '/myfirstjsp.jsp' 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 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 as a compiled Java class.
Next time you'll invoke the jsp it will be already compiled.
No it is not possible. Tomcat is an applicationserver.
To compile .java files you have to use the tools from the jdk.
Compilation of java classes are performed via JDK and javac (java compiler) command.
To compiling your code You need to use command line or IDE, then you can deploy it in AS (such as tomcat)
*Only jsp files automatically compiled in AS
Related
New to Tomcat. Running 7 and https://github.com/ajanata/PretendYoureXyzzy.
Everything runs good with exporting the WAR and deploying it, but when I try to launch the app I get an error that it cannot compile class for JSP. The error is here.
Eclipse has an error in the markers but I'm not sure if that's a problem. (Target runtime Apache Tomcat 7 is not defined.)
The only error that matters is:
Only a type can be imported. net.socialgamer.cah.data.GameOptions resolves to a package.
GameOptions is supposed to be a class or an enum.
Check the .jar file that contains GameOptions. It appears to be corrupt.
You may also need to check the WEB-INF/classes/net/socialgamer/cah/data folder of the .war file. It should not contain a folder called GameOptions, but may contain a file called GameOptions.class, if the classes were not packaged in a .jar file.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Apologies for the generic nature of this question.
I have been learning Java for a few months now, and I've created a few simple projects which have some functionality. I wanted to send the projects to a friend and I'm running into countless errors and struggles. Passing a .jar file is causing "Main class not found" errors when they try to open it.
I tried using third party software to wrap the .jar files into an .exe file and the same errors still persist.
Beyond that, I'm convinced that passing around .jar files wrapped into .exe files via third party software is NOT how Java was intended to be used. I've read two books on Java and they all talk about structuring the language, but I'm confused about WHERE I'm supposed to be using this code because it has become painfully obvious that it is NOT intended to be passed around in file format.
Is this a server programming language? Used on the back end of websites mainly? I'm not sure where one would be using the code written in Java.
You should build an executable jar.
Check here and here.
You need to create your JAR file as an executable JAR file if you want someone to be able to run it. That is how you send around Java executables. Look here for more info:
How do I create executable Java program?
I'd recommend you spend some time with http://docs.oracle.com/javase/tutorial/. Java program basically runs on top of virtual machine (not directly on your physical machine). Java program is compiled into *.class files, and a collection of *.class is commonly bundled into a *.jar file.
If you want to run your java program, one common way is to execute the java virtual machine runtime (JRE) and specify your jar package to be loaded into the classpath, eg:
java -jar /path/to/my.jar
Your jar file has to be packaged properly such that it indicates what is the main class (if any)
Packaging jar into exe is possible, but is not best practice. Java paradigm is a write & compile once -- runs everywhere.
from Oracle:
If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. You provide this information with the Main-Class header in the manifest, which has the general form:
Main-Class: classname
The value classname is the name of the class that is your application's entry point.
Recall that the entry point is a class having a method with signature public static void main(String[] args).
After you have set the Main-Class header in the manifest, you then run the JAR file using the following form of the java command:
java -jar JAR-name
The main method of the class specified in the Main-Class header is executed.
Setting an Entry Point with the JAR Tool:
The 'e' flag (for 'entrypoint'), introduced in JDK 6, creates or overrides the manifest's Main-Class attribute. It can be used while creating or updating a jar file. Use it to specify the application entry point without editing or creating the manifest file.
For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp:
jar cfe app.jar MyApp MyApp.class
You can directly invoke this application by running the following command:
java -jar app.jar
If the entrypoint class name is in a package it may use a '.' (dot) character as the delimiter. For example, if Main.class is in a package called foo the entry point can be specified in the following ways:
jar cfe Main.jar foo.Main foo/Main.class
Setting an Application's Entry Point
For a quick answer, libraries are bundled in jars, which are then added to the classpath of another application.
Pretend you invent a new sort algorithm which is faster than all the others. You could bundle up a small number of classes into a jar, which is basically just a zip file containing your compiled .class files.
Now your friend needs to sort some data and wants to use your classes. He would write his code to import your classes (using import at the top of his .java files), and then work with them in his code. When it came time for him to compile his .java files into .class files, he would add your jar to his classpath. Eclipse/netbeans can set this up for you, or if you're running javac from the command line it would look something like this:
javac ... -cp "fastsorting.jar" ...
Sometimes (rarely, in the real world) someone has a JAR which is really a fully fledged program, meant to be run. I say rarely, because jars aren't the most common way to distribute software. More popular is as a web service or through an applet on a website. In this case, a jar's manifest file (just a text file telling java information about this jar) will have a main class, which is run when the the jar is invoked through java.
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.
I am using Tomcat 7.1 and Eclipse Indigo to develop a Java Web Application.
I just want to ask if anybody knows where the JSP translated files (the java files) are stored? Its often i receive exceptions indicating the jsp java file line, but if i can not see the file it is more difficult to correct the bug.
Doubleclick the Tomcat entry in Servers tab in Eclipse. You'll see something like this in Server Locations section:
If you haven't changed the server path and it thus defaults to Eclipse's workspace metadata, then the Tomcat's /work directory is not in Tomcat's own installation folder, but in the path as shown in the above screenshot.
Unrelated to the concrete problem, if you adhere the JSP coding recommendations and you avoid Java code in JSP altogether, then debugging will be so much easier, along with a lot of other advantages.
The JSP translated files are stored (in Tomcat) in /work/Catalina/localhost/[your_app_context]/org/apache/jsp/.
Should check in other containers or web servers.
you can find the jsp compailed java classes in your workspace:
And the path is: workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\auctuus\org\apache\jsp\jsp\gbp\settings
I'm currently working on a prebuilt application running on weblogic.
The application consist in NAME_APPLICATION.jar that must be deployed on weblogic.
My problem is that I can't analyze anything for the simple reason that the jar DOES NOT CONTAIN ANY .JAVA OR .CLASS FILE
The jar just contain the following files:
- APPLICATION_1.0.sources
- APPLICATION_1.0.space
- APPLICATION.ws
- GET_SOMETHING.ds
- GET_SOMETHING.service
I really don't understand it. Where is the application ? How weblogic knows the logic (forgive me the pun) of the app? How to edit the application and where is the source files?
It's the first time that I see such Jar file, it's probably because I'm a weblogic beginner
Thanks
These are resources that must be available on the classpath for the application to find them and access them. I am guessing that the documentation tells you to deploy it as a shared library.
If they are binary files (not text files that you could try reading with a text editor), then whatever application needs them obviously knows their format and how to read them.