Build issue with eclipse web app - class

I have a web app created in eclipse. After some strange changes I made, which I can't remember, all classes under the build directory do not work. Specifically when I open them I get these stuff:
Class File Editor
Source not found
There is no source attached to the class file foo.class
// Compiled from foo.java (version 1.7 : 51.0, super bit)
public class Beans.foo implements java.io.Serializable {
// Field descriptor #8 J
private static final long serialVersionUID = 1L;
................
................
................
................
Although, the classes under the WebContent directory work fine.
(also tomcat can't start, and i think it is because of this)
How can I fix this?
EDIT: I copied and pasted all the java files from WebContent into build, but now eclipse understands the same java files (that exist also in build and in webContent) as different files.
thank you.

Related

Could not find main method from given launch configuration

I've a simple Java project that works when I execute it at Eclipse environment. But when I try to export it to a Runnable Jar, I get the following error:
JAR export finished with warnings. See details for additional information.
Exported with compile warnings: JavaSwing/src.main.java/com/cansoft/GUIProgram.java
Exported with compile warnings: JavaSwing/src.main.java/com/util/Util.java
Jar export finished with problems. See details for additional information.
Could not find main method from given launch configuration.
I read other posts which suggest to create a MANIFEST.MF file specifying the main-class which I did. It is placed at MyProjectFolder/META-INF/MANIFEST.MF and it contains the following information:
Manifest-Version: 1.0
Class-Path: resources
main-class: com.cansoft.GUIProgram
My main class is as follows:
public class GUIProgram {
private JFrame folderCreationSubappFrame;
private Color color;
private String home;
private final static Logger LOG_MONITOR = Logger.getLogger("com.cansoft");
public static void main(String[] args) {
try {
new GUIProgram();
} catch (Exception e) {
LOG_MONITOR.log(Level.INFO,e.getMessage());
}
}
public GUIProgram() throws InterruptedException, SecurityException, IOException {
home = System.getProperty("user.home") + File.separator + "Documents";
startLogSystem();
if(isFirstRun()) {
showWelcomeFrame();
} else {
initialize();
}
} .... More and more code
Does anybody know what am I missing? Any help much appreciated.
Thank you.
It is not enough to create the manifest file, you need to explicitly choose it in the Eclipse jar export dialog.
Answer to Comment
If you use "runnable jar", make sure that you chose the correct launch configuration and that the launch configuration successfully runs when chosing "Run As" -> "Run Configurations" -> "Java Application" -> Your Configuration -> "Run"
I finally find out where the problem was, it was quite simple btw. I had created my GUIProgram within a src.main.java package, but that package was created (my bad) as resources instead of folders, so Eclipse was smart enought to run it but when trying to generate the JAR which expected a correct java project structure, it was failing because truly there were not GUIProgram java class at src path (src was not folder type but resources).
Hope I succeed explaining.

Not able to create executable jar for webdriver+TestNg project which does not contain main method in any class

I am learning webdriver and I have created one project with TestNg.
I have several classes in my package under src folder.
No class contains public static void main(....). i.e[Entry Point]
My question is :
Can we create Runnable / Executable jar file through eclipse for projects like this[project without main method]. I searched on many sites but unfortunately didnt get solution.
Please suggest me some links OR The way by which we can do this.
To create a jar file of the TestNG without main method you have to create another class which contain main method.
Suppose you have a TestNG file name as Sample.java, in the same package create one more class as ExecutableRar and write the below code :
public class ExecutableRar {
public static void main(String[] args) {
TestNG testng = new TestNG();
Class[] classes = new Class[]{Sample.class};
testng.setTestClasses(classes);
testng.run();
}
Now you can run this class as a Java Application. Then right click on the package --> Export --> Java --> Runnable jar File --> select ExecutableRar in launch configuration --> Browse the file location and enter the name of the file in Export Destination --> Finish.
Please let me know if you are having any issues.

Use webapp classpath using JavaCompiler in Tomcat within Eclipse with Maven

I have an existing "Example Webapp" that references "Example Library" using Maven. I'm running Tomcat 7 inside Eclipse 4.3RC3 with the m2e plugin. When I launch Example Webapp on Tomcat inside Eclipse, I have verified that the example-library.jar is probably getting deployed in the Tomcat instance's WEB-INF/lib folder.
The Example Webapp has code that compiles certain classes on the fly using JavaCompiler.CompilationTask. These dynamically generated classes reference classes in example-library.jar. Unfortunately the compile task is failing because the referenced classes cannot be found.
I understand that I can set the JavaCompiler classpath, but System.getProperty("java.class.path") only returns me the Tomcat classpath, not the webapp classpath:
C:\bin\tomcat\bin\bootstrap.jar;C:\bin\tomcat\bin\tomcat-juli.jar;C:\bin\jdk6\lib\tools.jar
Other have said that I need to get the real path of WEB-INF/lib from the servlet context, but the class generation code doesn't know anything about a servlet context --- it is written to be agnostic of whether it is used on the client or on the server.
In another question, one answer indicated I could enumerate the classloader URLs, and sure enough this provides me with the jars in WEB-INF/lib, but when I provide this as a -classpath option to compiler.getTask(), the task still fails because it can't find the referenced classes.
How can I simply provide the classpath of the currently executing code to the JavaCompiler instance so that it will find the classes from the libraries in WEB-INF/lib? (A similar question was raised but never answered regarding referencing jars within ear files using JavaCompiler.)
Example: In an attempt to get things working at any cost, I even tried to hard-code the classpath. For example, I have foobar.lib in my webapp lib directory, so I used the following code, modified from the answers I indicated above:
List<String> options = new ArrayList<String>();
options.add("-classpath");
options.add("C:\\work\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\FooBar\\WEB-INF\\lib\\foobar.jar");
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
boolean success = task.call();
In the end success is false, and my diaognostics indicates package com.example.foo.bar does not exist..., even though that package is in foobar.jar.
Put example-library.jar somewhere in your file system and pass that location to the code that runs JavaCompiler (the -classpath option). If you use an exploded WAR file to deploy, you can of course point it to the physical location within the WEB-INF/lib folder. The point is that you only need one configurable parameter in your webapp to do this, which can be a properties file entry, -D system property, database row or something else entirely.
Sample code (tested in Tomcat 7 and OpenJDK 1.7 on Fedora 18 x64):
private File compile(File javaFile, String classpath) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjects(javaFile);
List<String> options = classpath != null ? Arrays.asList("-classpath", classpath) : null;
StringWriter output = new StringWriter();
try {
boolean successful = compiler.getTask(output, fileManager, null, options, null, compilationUnit).call();
if (!successful) {
throw new CompilationException("Failed to compile: " + javaFile, output.toString());
}
return firstClassFileFrom(javaFile.getParentFile());
} finally {
fileManager.close();
}
}
private File firstClassFileFrom(File directory) {
return directory.listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
return name.endsWith(".class");
}
})[0];
}
See https://github.com/jpalomaki/compiler for a runnable sample webapp.
i met the same question. The reason is not "-classpath" .
my code :
String classpath ="xx/WEB-INF/clases ";
List<String> options = classpath != null ? Arrays.asList("-d", classpath,"-cp",classpath) : null;
CompilationTask task = compiler.getTask(null, javaDinamicoManager, diagnostics,
options, null, Arrays.asList(sourceObj));
boolean result = task.call();
the “result” will return true .
You could follow the answer provided for the even more specific question of how to load dependencies of compiled code from within a web app running directly from an unexpanded WAR file (there are no JAR files to reference - only the container's class loder knows how to access the classes): https://stackoverflow.com/a/45038007/2546679

Eclipse Tomcat integration: useBean class attribute [class] is invalid

I'm using Eclipse Juno (Java EE) and Tomcat 7.0. I've successfully connected Tomcat and Eclipse with this tutorial: http://www.mulesoft.com/tomcat-eclipse
JSTL and *.jsp files are working well within Eclipse and Tomcat but I can't use JavaBeans.
I store my JavaBeans in /WebContent/WEB-INF/classes/com/form.
In the *.jsp File (which is in /WebContent) I use the following code:
<%# taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%# page import="com.form.*" %>
...
<jsp:useBean id="input" class="com.form.Input"/>
Input.java:
package com.form;
public class Input {
private String firstName;
private String lastName;
private String gender;
private boolean vegetarian;
public Input() {
this.firstName = new String();
this.lastName = new String();
this.gender = new String();
}
...
When I run this application I get the following error message:
The value for the useBean class attribute com.form.Input is invalid
I don't want to post the whole code but you can download the complete Eclipse project here: https://dl.dropbox.com/u/6454333/BeanForm.zip
I hope you can help me.
Thank you and good bye
konze
You're confusing source files and class files.
WebContent is the place, in Eclipse, where you put the resources of the webapp (html files, images, css files, jsp files, etc.). There shouldn't be anything under WebContent/WEB-INF/classes. No .java file. No .class file.
When building the application with Eclipse, Eclipse will compile all the .java files in the project source directory, and the generated .class files will be part of the runtime classpath of the deployed web application.
If you generate a war file from eclipse, it will contain a WEB-INF/classes directory which will contain the .class file it has generated when compiling the source files from the project source directory.

Build Path issue with Java Android project when starting Eclipse

I booted up eclipse to start on a project, and without typing in a word I already have several errors.
package department.namespace;
import android.app.Activity;
import android.os.Bundle;
public class DepartmentActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
At package department.namespace, it says:
multiple marks at this line and the type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
The import android.os cannot be resolved.
Override cannot be resolved as a type
The method OnCreate(Bundle) is undefined for the type Activity
R. cannot be resolved as a variable.
Since this JUST came up when I started Eclipse, how do I fix this?
First, you should start by cleaning and building the project. This can be done by selecting the project of interest and then selecting the appropriate option from the project menu.
If that doesn't resolve the issue, then I would recommend checking the projects build path to ensure that your expected dependencies are present and accounted for. If I remember correctly when I have had this issue in the past, it helps to remove and re-add the JRE of choice.
To look into this issue further, you might check some of the following links:
Android Dev Specific - http://kyleclegg.com/eclipse-android-error-type-cannot-be-resolved/
http://dev-answers.blogspot.com/2009/06/eclipse-build-errors-javalangobject.html
http://www.adriancourreges.com/articles/the-type-java-lang-object-cannot-be-resolved-with-eclipse/