This is my project structure in IntelliJ IDEA:
I can use below code to get the webapp path, but I don't know how to get the java path.
${pageContext.request.contextPath}
Someone know how to get the path?
The closest information you can have is the package of a class that you can get with Class.getPackage().
public Package getPackage()
Gets the package for this class. The class loader of this class is used to find the package. If the class was loaded by the bootstrap class loader the set of packages loaded from CLASSPATH is searched to find the package of the class. Null is returned if no package object was created by the class loader of this class.
Packages have attributes for versions and specifications only if the information was defined in the manifests that accompany the classes, and if the class loader created the package instance with the attributes from the manifest.
Returns:the package of the class, or null if no package information is available from the archive or codebase.
Of course this won't be the absolute path.
PS : Not sure what you want to do with this but this should be what you want.
I think you need to know some basic rules. JSP is used from front end, thus is used for client side. Although JSP in turn transforms into executable Java Servlets and itself interacts with server(dynamic pages), but you need to understand that if the JSP has a direct source code linkage, then what security will java provide. Anyone can write the malicious code and interact with the actual source code. Thats why it uses the byte code (compiled code) and have its own directory structure.
Hope that helps
Related
Thanks for helping me out
Today I have a very simple problem
Problem:
On my application startup, I am loading all the classes inside one package using
Class.forName("org.codehaus.jackson.JsonGenerator$Feature");
............................
............................ and so on
Like this I loaded all the classes, everything is fine, until I upgrade the Jar, so the Jar package is updated to some other package name, from org.codehaus.jackson to com.fasterxml.jackson. So I have to change class.forName code.
Solution required:
Is the following code is possible
Class.forName("com.fasterxml.jackson.*");
or there is any other way to load all the classes under one package?
please help :)
The Class.forName("com.fasterxml.jackson.*"); is not possible,
docs are very clear about it, that the name parameter should be:
name - fully qualified name of the desired class
In addition the Class.forName returns a single object representing a class behind given name.
Your question has that, if you upgrade/update jar file the packages of jackson can be one of org.codehaus.jackson or com.fasterxml.jackson. This is weird if you want to load the jackson classes dynamically and you are not sure what will be the base package for those classes.
But anyway, a kind of solution would be to scan with Reflections if the given package name contains required classes (or at least few of them, so you would know that the package is visible) like here:
Can you find all classes in a package using reflection?
And later loop through the package with classes to initialize them with Class.forName.
I'm working now together with others in a grails project. I have to write some Java-classes. But I need access to an searchable object created with groovy. It seems, that this object has to be placed in the default-package.
My question is: Is there a way to access this object in the default-package from a Java-class in a named package?
You can’t use classes in the default package from a named package.
(Technically you can, as shown in Sharique Abdullah's answer through reflection API, but classes from the unnamed namespace are not in scope in an import declaration)
Prior to J2SE 1.4 you could import classes from the default package using a syntax like this:
import Unfinished;
That's no longer allowed. So to access a default package class from within a packaged class requires moving the default package class into a package of its own.
If you have access to the source generated by groovy, some post-processing is needed to move the file into a dedicated package and add this "package" directive at its beginning.
Update 2014: bug 6975015, for JDK7 and JDK8, describe an even stricter prohibition against import from unnamed package.
The TypeName must be the canonical name of a class type, interface type, enum type, or annotation type.
The type must be either a member of a named package, or a member of a type whose outermost lexically enclosing type is a member of a named package, or a compile-time error occurs.
Andreas points out in the comments:
"why is [the default package] there in the first place? design error?"
No, it's deliberate.
JLS 7.4.2. Unnamed Packages says: "Unnamed packages are provided by the Java SE platform principally for convenience when developing small or temporary applications or when just beginning development".
In fact, you can.
Using reflections API you can access any class so far. At least I was able to :)
Class fooClass = Class.forName("FooBar");
Method fooMethod = fooClass.getMethod("fooMethod", String.class);
String fooReturned = (String)fooMethod.invoke(fooClass.newInstance(), "I did it");
Use jarjar to repackage the jar file with the following rule:
rule * <target package name>.#1
All classes in the default package of the source jar file will move to the target package, thus are able to access.
You can use packages in the Groovy code, and things will work just fine.
It may mean a minor reorganization of code under grails-app and a little bit of a pain at first, but on a large grails project, it just make sense to organize things in packages. We use the Java standard package naming convention com.foo.<app>.<package>.
Having everything in the default package becomes a hindrance to integration, as you're finding.
Controllers seem to be the one Grails artifact (or artefact) that resists being put in a Java package. Probably I just haven't figured out the Convention for that yet. ;-)
just to complete the idea:
From inside default-package you can access objects resided in named packages.
I've already read the tutorial at ELKI documentation ( http://elki.dbs.ifi.lmu.de/wiki/Tutorial/DistanceFunctions ).
Unfortunately, I'm not grasping how to plug the generated .class with MiniGUI (or bash script for the provided .jar). How it can be done?
Ps: I know it sounds absolutely noob, but when I try to "type" the class name, as suggested, I get the error "The following parameters could not be processed: HammingDistance", for example.
ELKI will load classes via the standard Java Classloader. Therefore, they must be on the class path or they cannot be loaded. An example call (assuming your classes are in the bin folder) is java -cp elki.jar:bin/ de.lmu.ifi.dbs.elki.application.ELKILauncher
Parameters are interpreted as follows:
If there is a class with this name (including the package name!) it is used.
Otherwise, ELKI tries prepending the package name of the expected interface. Which enables shortcut names.
Otherwise, known classes (from the service files) are checked for aliases. For example, the Euclidean distance has an alias name of l2, Manhattan has an alias l1.
The class must have a parameterless public constructor or a inner public static class Parameterizer.
Input assistance is built as follows:
.jar files on the classpath are checked for service files in META-INF/elki/<interface>
folders on the classpath put you in development mode, where a recursive list is performed and all .class files are inspected. This is much slower, but removes the need to edit the service files. Discovered classes show up below the ones listed in the service file.
Furthermore, the package de.lmu.ifi.dbs.elki.application.internal includes classes that will inspect everything on your classpath, and will report e.g. classes that do not have a parameterless public constructor, or a inner public static class Parameterizer.
I found this question -> Import custom libraries in Java
And #Andy Thomas-Cramer said that the classes in "stdlib.jar" from "An introduction to programming in Java" have no packages, so they are in the default package.
Isn't this a bad practice? If you have something with no package the IDEs' auto-completion is quite slower. And also this means that we could not use any of the classes, in that jar, from classes with packages different then the default?
Can someone please tell me how we could deal with this?
EDIT:
I have 2 jars and I put them in Referenced libraries, they both have a bunch of classes in default package. When I create class in different package then the default - lets say org.myquestion I can't access the classes from the jars anymore.
This is something that really bugs me... First I can't create my own package and use anything from the jars. Second my IDE's (I use eclipse) auto-complete goes terrible - I guess it searches to meany classes at once... What I want to do is to put somehow the jars in some namespace... and to be able to access them like org.someones.libs.SomeClass
It certainly is bad practice to use the default package. A package groups classes and provides them with access protection (protected, package private) and functions as a unique namespace.
You can always use classes from every package, them being default or not, you can always mix.
Download the jar source code, And built it to jar by yourself and added the package name whatever your like.That's will solve your problem.
Importing classes inside JAR files that are in the default package
I ran into the exactly same problem as you did. The problem is the jar file "stdln.jar" has no named package, say, only with default package.
You cannot import a class from a default package, basically, since the import operation needs the package name:
import packagename.*;
So there are only two way to fix this problem:
the easier one: Do not create a package in your src folder and use default package two! Every class in stdln.jar would be imported to your src automatically.
Like this:
enter image description here
try to create your own jar file with a named package and copy all the class file into your newly-created jar file.
Since the stdln.jar is only used for education, so which you are gonna choose does not really matter. In real development, we never use default named package since it's not really a good practice, always leading to some confusing stuff.
Hope this would help you!
I have imported the algs4.jar file as a referenced library as seen above. All seems to be fine but I can't seem to use or access any of the classes in the library.
Is there an import statement I'm missing?
Image reference:
Note how I'm trying to use BinarySearch from the referenced library but it is not offering me an option to use or import it.
I am taking an Algorithms class via Coursera that utilizes the algs4.jar reference library.
The Problem:
The issue you are having is that the reference library is located in the default package and the source in which you are accessing the reference library is not.
The Solution
All classes in your project need to be in the default package, otherwise the reference libraries (which are located in the default package) will not be recognized. Your project classes need to be in the src directory and you should not declare a package at the beginning of your .java files. Additionally you also need to make sure you've added the reference libraries to your build path.
I am not sure, I did not try this. Some interpretations though.
Is this a standard library or someone created it by himself and gave you?
The problem is class BinarySearch is in default package. You will not be able to import it. If this library is created by you, or by any of your friend, you need to ask your friend to move all classes from default package to a good namespace and then re-create the jar.
If class is accessible, ctr + shift + o should import the class.
Note that the classes in the jar files are all contained in the default package.
If your classes are then contained in a package then they will not be directly accessible.
I submit this answer because I am taking the algorithms course to which these jar files belong and this was the specific cause of my program not working.
If you want to import the classes, you need to download the packaged version of the library. See the Q&A section at the bottom of this webpage.
http://introcs.cs.princeton.edu/java/stdlib/
Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar. Why not?
A. The libraries in stdlib.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use our libraries with a named package, you can use the packaged version stdlib-package.jar.
Firstly, BinarySearch can not be created (private Constructor). Use the static Method BinarySearch.rank(int key, int[] array)
Secondly you can access BinarySearch only from default package
Thirdly what about Arrays.binarySearch(array, key) ?
Just delete module_info.java file and the referenced libraries are accessible instantly