Jar files in JSP not loading properly - java

This might be a duplicate, but I cannot find an answer using the below answers and many other sites on the internet...
My conundrum:
I am attempting to (poorly) run some classes from a jar in jsp. Effectively what I have is the following:
<%#page import="edu.cs242.hadoop.*" %>
<%
... do some stuff ...
MRSearcher ss = new MRSearcher();
... do some stuff ...
%>
But every time I try to run the jsp I get the following error:
An error occurred at line: 32 in the jsp file: /hadoop.jsp
MRSearcher cannot be resolved to a type
My webapp structure looks like:
/
|hadoop.jsp
|lucene.jsp
|index.jsp
|WEB-INF/
|lib/
|lucene.jar
|hadoop.jar
|classes/
|*.java for our hadoop.jar
I've tried calling the jar itself and compiling the java through tomcat, both produce the same results.
Here is a snippet from our MRSearcher class:
package edu.cs242.hadoop;
import java.io.*;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
/**
* Created by cloudera on 3/10/14.
*/
public class MRSearcher {
MRSearcher() {}
public String[] run(String arg1, String arg2) {
String[] things = new String[] {};
// do stuff
return things;
}
}
There are other classes, but this one is the one that allows us to interface with the rest of the program. The main is in a file called: Main.java, and it does nothing but runs this for command line output. The syntax is correct as we can run the main and retrieve output.
I don't mean to sound insolent, but please don't comment on the futility and awfulness of including things like JAR files in JSP. This is never going to production, it's a school project that doesn't need the necessity of correctness, it needs the necessity of functioning. If I were doing this for a job I would do it right, but right now I don't care about learning about the correct way to separate logic and presentation layers in JSP -- I can do that just fine in other languages and understand the concept very well.
I have looked through and attempted to use the following solutions before posting this, all of this has failed:
how to run jar file methods in jsp
How to call method from jar file in JSP?
how to reference an external jar in jsp app?
And more to try to solve this problem.

In WEB-INF/classes/ you must have MRSearcher.class file. Keep in mind JSP files are compiled first time you access it, so your project can compile although your JSP are not well.
On the other hand, your MRSearcher() constructor must be public, if you omitte this, by default the constructor will be package and cannot be accessed from other packages. Check if your Main.java class is in same package of MRSearcher. If yes, this is why it can invoke MRSearcher constructor.
I hope this helps you.
Regards,

Related

How do you alias an imported package in a JSP file?

I have a JSP file that has a few imports at the top:
<%# page import="org.json.JSONObject" %>
However, I have an issue because of a prior import resulting in a collision error with another import.
Is it possible to alias this import like you would in a traditional Java class?
import org.json.JSONObject jsOb
Note:
I know that there are no traditional aliasing mechanisms in Java. I just was not sure if there were some tag-based mechanism that would suffice. Additionally, using a fully qualified path to the package will not work, since the import is actually failing.
I am not aware of any aliasing mechanisms for packages in Java. The following is obviously illegal in Java:
import org.json.JSONObject;
import com.mypackage.JSONObject;
If there is a conflict in names in Java using 2 or more classes sharing the same name, you have to distinguish them using the full package name:
import org.json.JSONObject;
// code
JSONObject json = ...;
com.mypackage.JSONObject jsonObject = new com.mypackage.JSONObject(json);
The same analogy has to be used in the Java Servlet Pages. The best solution would be to avoid those classes with the same name if possible.

How to use JLex.CAlloc.class

I downloaded and imported xalan-2.5.0.jar to my netbeans project in order to use the CAlloc.class function. But i cant figure out how to use the CAlloc function.
i imported the package by doing this,
import JLex.*;
i wanted to import specifically the calloc class but i get error when i tried this
import JLex.CAlloc;
the error says "CAlloc is not public in JLex; cannot be accessed from outside package" , i opened and checked the CAlloc.class and it not public but i cant edit it, may be if there is a way to edit the class ???

Java import enumeration

Hi I had a program which worked fine in a .jar file.
Basically all the classes were part of the same package called : "eu.floraresearch.lablib.gui.checkboxtree"
They ALL are located in the SAME "checkboxTree" folder. Each file has a line stating
package eu.floraresearch.lablib.gui.checkboxtree;
I need to modify the code and to integrate it in another project.
So I took all the .java files, copied them in my Eclipse project folder (no package anymore), and got rid of the "package eu.floraresearch.lablib.gui.checkboxtree;" line in each files. Everything is fine except for one file comprising an enumeration.
Originally the file looked like this:
package eu.floraresearch.lablib.gui.checkboxtree;
public class QuadristateButtonModel extends DefaultButtonModel {
public enum State {
CHECKED, GREY_CHECKED, GREY_UNCHECKED, UNCHECKED
}
...
}
The problem is, there is another class which originally imported the above class enumeration:
import eu.floraresearch.lablib.gui.checkboxtree.QuadristateButtonModel.State;
public class QuadristateCheckbox extends JCheckBox {
public QuadristateCheckbox() {
this(null);
}
public QuadristateCheckbox(String text) {
this(text, State.UNCHECKED);
}
...
}
First of all I find it weird how enumerations can be imported.. But it worked fine when everything was inside the package.
Since all my .java files are in the same folder now, I just removed the package line.
However I have this issue with the QuadristateCheckbox class which imports "QuadristateButtonModel.State".
If I change the import line with
import QuadristateButtonModel.State;
it states
The import QuadristateButtonModel cannot be resolved
I tried various things I found on the internet like
import static QuadristateButtonModel.State.*;
or
import QuadristateButtonModel.State.*;
but the same error messages occur:
The import QuadristateButtonModel cannot be resolved
On top of that, in the above code from QuadristateCheckbox class:
public QuadristateCheckbox(String text) {
this(text, State.UNCHECKED);
}
an error message
State cannot be resolved to a variable
which is understandable given the fact that I fail to import the State enumeration.
What can I do? Please explain to me what is wrong
PS: The code was taken from this site: http://www.javaworld.com/article/2077762/core-java/swing-based-tree-layouts-with-checkboxtree.html
The authors provide classes to build checkable trees.

Receiving multiple syntax errors when using registerServlet method

I am trying to implement JUnit unit testing using a ServletUnit to test a servlet, specifically a java file named SignedNotesServlet.java . This test class is in the same directory as SignedNotesServlet.java . I am using Eclipse.
However, I am having trouble writing the correct syntax for registerServlet method that is part of ServletUnit and HttpUnit. I have not yet run the program. The errors I am receiving are
Syntax error on tokens, FormalParameter expected instead
Syntax error on token "class", identifier expected
Syntax error on token(s), misplaced construct(s)
Syntax error on token ""SignedNotesServlet"", invalid FormalParameterList
Here is my code:
package notetaker;
import com.meterware.servletunit.ServletRunner;
import com.meterware.servletunit.ServletUnitClient;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.fail;
public class SignedNotesServletTest {
ServletRunner sr = new ServletRunner();
sr.registerServlet( "SignedNotesServlet", SignedNotesServlet.class.getName() );
private SignedNotesServlet signednotes;
#Test
public void test() {
fail("Not yet implemented");
}
}
I think I correctly added the jar file the buildpath, using instructions from http://tinyurl.com/ku7huss . I used http://tinyurl.com/b55fn as my main reference but am also looking at the few registerServlet examples on the web (I can't post those links for reference since I need a 10 reputation to post more than 2 links). I am not entirely sure what could be wrong since I basically copy-pasted from the second website and made (what I thought) were appropriate changes.
I also thought that maybe something was wrong with "SignedNotesServlet" since there were quadruple quotes in the error, and I removed them, but it still doesn't work out, and I don't think that would have been correct syntax anyway, based on the examples.
This is the problem:
sr.registerServlet( "SignedNotesServlet", SignedNotesServlet.class.getName() );
This statement isn't in a method or constructor - it needs to be. Either put it within the test itself or put it in a setup method. If you put that code into the individual test, I'd also recommend making sr a local variable for that test. If you put it into a setup method, you'd need sr to be an instance variable still, but I'd suggest making it private and giving it a more useful name.
I am not entirely sure what could be wrong since I basically copy-pasted from the second website
But you copied it into an inappropriate place. Note that this has nothing to do with servlets, JUnit, ServletUnit or your build path - it's simply invalid Java.

Why don't I have to import a class I just made to use it in my main class? (Java)

I am currently learning Java using the Deitel's book Java How to Program 8th edition (early objects version).
I am on the chapter on creating classes and methods.
However, I got really confused by the example provided there because it consists of two separate .java files and when one of them uses a method from the other one, it did not import the class. It just created an object of that class from the other .java file without importing it first.
How does that work? Why don't I need to import it?
Here is the code from the book (I removed most comments, to save typing space/time...):
.java class:
//GradeBook.java
public class GradeBook
{
public void displayMessage()
{
System.out.printf( "Welcome to the grade book!" );
}
}
The main .java file:
//GradeBookTest.java
public class GradeBookTest
{
public static void main( String[] args)
{
GradeBook myGradeBook = new GradeBook();
myGradeBook.displayMessage();
}
}
I thought I had to write
import GradeBook.java;
or something like that.
How does the compiler know where GradeBook class and its methods are found and how does it know if it exists at all if we dont import that class?
I did lots of Googling but found no answer.
I am new to programming so please tolerate my newbie question.
Thank you in advance.
It is because both are in same package(folder). They are automatically imported no need to write import statement for that.
You don't have to import classes that are in the same package as the current class.
Also, note that GradeBook.java is the name of the file. The (simple) name of the class is GradeBook. Every class should be in a package. If it is in the package com.foo.bar, the class name is com.foo.bar.GradeBook, and this is the name you must use when importing this class.
Read http://download.oracle.com/javase/tutorial/java/package/packages.html to learn more about packages.
The classes located in the same package do not have to be imported, as they are visible to each other. You simply import a class that is in another package:
import java.util.ArrayList;
Note that you are not importing the file, but the class.
It's all about packages. You are trying to use a class from the default package which does not need explicit import of the java file, ie GradeBook inside GradeBookTest
Here is where you can start with learning about packages :
Java Package Tutorial
and :
Creating and Using Packages
Java doesn't use includes the way C does. Instead java uses a concept called the classpath, a list of resources containing java classes. The JVM can access any class on the classpath by name so if you can extend classes and refer to types simply by declaring them.
From: Include one java file in another java file
Imports are for importing classes that are in a different package. Since you didn't declare a package for either they are both put in the default package. The compiler can find it because the class lives in the same directory.
You don't have to import classes which are in the same package.
Well, classes in the same package are automatically imported.
They're in the same package. This tutorial will do more justice than I will.

Categories

Resources