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

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.

Related

Import java.nio.file.Files and com.google.common.io.Files in 1 file is not allowed

I'm very new to java I need to use a different features from MAVEN dependencies but they have a same name like this,
import java.nio.file.Files;
import com.google.common.io.Files;
I do not allow me to import. I will throw error like
The import com.google.common.io.Files collides with another import statement
Can this be solved ? Thanks a lot.
When you have to use two classes with the same name, you have to import one and use the fully qualified name of the other one in the code.
For example, leave the first import. And when you want to create one variable of each type, you do the following:
import java.nio.file.Files;
public class MyClass{
Files files; //This variable uses the imported type
com.google.common.io.Files ioFiles; //This variable uses the explicit type
}

Java doesn't have any import aliasing mechanisms, and I have two import statements with the same name

I am using selenium for web interaction and I need to use import com.google.common.io.Files for it to work. However, I also need to use import java.nio.file.Files; for some files that I work with. I don't know what to do because java doesn't allow for import aliasing as far as I can tell. Is there any way to cope with this? Let me know if I need to provide any other details
The "import java.nio.file.Files:" is only used for the ".readString(Path)" So if there is another way to get the contents of a file as a string from a "Path" please let me know.
The import java.nio.file.Files; is also used only inside a method if there is any weird obscure way to only import for the method.
You can import one File class to access directly by its name. Furthermore you can access your second File class by its full qualified name. e.g.
import com.google.common.io.Files;
...
Files f = new Files()
f.yxz();
java.nio.file.Files a = new java.nio.file.Files()
a.abc()
The workaround is to import one and use the fully qualified name for the other.
import java.nio.file.Files;
MyClass {
Files.list(...); // refers to Files from java.nio
com.google.common.io.Files.getFileExtension(...)
}

Facing a problem with using Natty library for converting string to date [duplicate]

I get the following error despite it's exactly one used in examples:
error: type List does not take parameters
List<String> strings_wat = new ArrayList<String>();
Java is version 1.7 and the class is NOT named ArrayList.
You are likely importing java.awt.List.
You should instead import java.util.List, which is a parameterized type.
It seems like you are importing it from java.awt:
import java.awt.List;
and it looks like you want to use the one from java.util:
import java.util.List;
if you are working on Graphical user interface you have to
import java.awt.List instead of import java.util.List
Other then else if you are working on simple code you have to import java.util.List; instead of import java.awt.List
I am getting solved this problem by renaming the class name or file name as in Notepad++.
You Should not take the class names as ArrayList or List.
And you also need to add the following package
import java.util.*;
My Previous class name is List I changed it into the Names.so You have to change the class name.

How do I import packages in Java?

I have a "Sprites" folder with some class files and a "Launcher" folder with some class files. I tried the following code for import:
package Sprites;
and it lead to the following
hw9\Launcher>javac *.java
TowerDefense.java:2: error: class, interface, or enum expected
package Sprites;
^
1 error
Am I doing this incorrectly? My Sprites and Launcher are in the hw9 directory, so I assumed it would work. A picture for clarification:
You can use a wildcard import to import all classes within the immediate directory:
import Sprites.*;
This opposed to something like:
import Sprites.Class1;
import Sprites.Class2;
import Sprites.Class3;
...
Generally, wildcard imports can produce conflicts and errors (for example java.awt.List and java.util.List), so usually better to avoid them.
Packages should also be lower-cased.
The error is due to syntax, usually when you see something like ...expected that is syntax error indicator.
In the class in your launcher package, include the import statements for the classes which are being referred to.
It should look something like the following:
package the.name.of.your.package;
import Spirites.NameOfclass; //quialify the import parth as is
class YourLauncherClass{
//class definition
}
Also make sure that semicolons aren't missing at the end of import and package.
Hope that helps.
Best practice is to import the specific class you require rather than importing the complete package.
import Spirites.NameOfclassRequired;
class YourClass{
//class definition
}
If you are using eclipse you can do that using CTRL+SHIFT+O When you do that eclipse imports the specific class you require. For an instance if you using an ArrayList rather than importing java.util.*; it will import java.util.ArrayList;
If you need multiple classes from a package then for sure you can import the entire package

Jar files in JSP not loading properly

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,

Categories

Resources