Class reference is taken from different jar - java

There are some classes used in a java program referred from abc package from xyz.jar. The package is imported in the java file.
Also the same class is in other lmn.jar.
So if I delete the jar file from the project, i should be getting the error.
But the class compiles and takes the class from the other lmn.jar.
Eg.
weblogic.jdbc.oci.Blob is a class in weblogic.jar
But if i delete weblogic.jar, it takes it from java.sql.Blob.
I don't want this to happen, the program should display error.

In such a case you can use fully qualified name of class, which will include package name. For example, instead of:
Blob blob = new Blob();
You can write:
weblogic.jdbc.oci.Blob blob = new weblogic.jdbc.oci.Blob();

When importing a class you are also defining the package. So when you import "weblogic.jdbc.oci.Blob" and remove this class from classpath it will not automatically import it from a different package unless you change the import statement as well.
Some IDEs might automatically try to resolve classes and add the missing import statements. Maybe check that.

For one of the case try to use the entire path, ie
For example if you need Blob from weblogic.jar
then try to call weblogic.jdbc.oci.Blob bl = ...

Related

How to write an import statement to refer to a java class in a jar?

How can I refer to a Java class in stdlib1.jar when the directory structure is like this? How to write the import statement?
I want to call a method under stdlib1.jar, I have configured it.
The classes are in the default package. According to this answer, it is not possible to import classes from the default package. So, they have to be moved to another package or you have to use reflection.
You call a method from a class and not from a package.
You don't need to specify the jar when you call a method from a class belonging to it. Which matters is your jar is in the classpath
In your screenshot if the lib makes part of the classpath folders, you can import and use classes from it in your code.
Here the classes of your jar use the default package (no package name) which seems weird for a third-party library. Default package is not recommended since it doesn't allow to naturally reference and use the classes of the archive from the client code.
I am not sure you are using the correct version of the jar. Look at that :
http://grepcode.com/snapshot/repo1.maven.org/maven2/com.googlecode.princeton-java-introduction/stdlib/1.0.1
This contains classes in the edu.princeton.cs package :
With package, you could declare this :
For example :
You could create a class like that and use BinaryIn like that:
package main;
import edu.princeton.cs.BinaryIn;
public class MyClass(){
public static void main(String args[]){
BinaryIn in = new BinaryIn();
}
}

Can't reference generated-sources class outside of default package

I'm working with Google's Protocol Buffer (in combination with the Protocol Buffers maven plugin) which compiles a .proto file into a class. I can use the generated class in the default package perfectly, but not outside of it. I don't really know how to explain it any better so I'm going to show you some pictures.
I've tried subclassing the Hrp class but that doesn't work (the generated class is final). It is also not an option to move the class every time I re-generate the Hrp class.
I'm not sure if this is relevant, but the generated class is public final. It contains an empty, private constructor.
I have also tried setting the generated sources package prefix for the generated sources folder but that also does not work.
Any help would be greatly appreciated.
Try adding a package id to your Protocol Buffers definition. See Protocol Buffers Package
i.e.
syntax = "proto3";
package MyPackage;
option optimize_for = SPEED;
message Product {
repeated ASale sale = 1;
}
Then when you Generate the Java~Protocol~Buffers code (using protoc), it will be in package MyPackage and you will be able to import it into your java code in the normal way.
In java, you can not import anything from the Default package; which I believe is your problem. See How to access java-classes in the default-package?

How to instantiate a class without knowing its package?

I have an Eclipse project (MainProject) and it references another Eclipse project (ReferencedProject). MainProject also references a JAR file (ReferencedJar). This ReferencedJar's file name is known. I also know there is a class (ReferencedClass) in ReferencedJar, but I don't know in what package ReferencedClass is because the package path is not known beforehand.
I need to instantiate ReferencedClass in ReferencedProject using reflection. How can I do this? And will the solution be okay when the project is packaged to a its standalone jar outside Eclipse?
The reason for this question is; The ReferencedJar is file generated by a modeller application. It generates java classes for your model and puts them into ReferencedJar. The user can choose which package the classes it generates will be put into. But the class names are always the same. MainProject is project that will include this generated jar, but ReferencedProject (a framework) also needs to instantiate a class in this generated jar. Hope this makes the question more clear.
Thanks in advance
Edit: Actually I have an idea but don't know how to implement it. Because I know the name of ReferencedJar file, I could access it on runtime and check all the classes it contains. Then I can find the matching class by name comparison. But how can I access the ReferencedJar on runtime?
As long as the class you need is in the class path, you can get a reference to it by invoking Class.forName(String className)
String className = "WhatEver";
String packageName = "some.package";
Class<?> c = Class.forName(packageName + "." + className);
If you don't know the package name, however, and there's no way to get it from a configuration file, I would recommend using a library like reflections to scan the class path and find the relevant class.
You can not instantiate a class you if do not know its package

Access classes from package

I'm developing an android test app and i'm going to access all internal class of android.view package. android.view is a package that is present in jar file. I tried by loading package name but it doesn't display the classes if any one tried
this already, please help.
Here's what I tried so far:
public static void main() throws ClassNotFoundException{
Class o =Class.forName("android.view");
Class[] C=o.getDeclaredClasses();
for(int i=0;i<C.length;i++) {
Classname = C[i].getName();
ClassesDisplayActivity.your_array_list3.add(Classname);
Log.i("Ramu","classname "+ C[i].getName());
}
}
}
It is not possible to determine at runtime all of the classes that are in a package using a standard class loader.
You might have some luck with this library though:
https://code.google.com/p/reflections/
Package is not a class. You cannot call Class.forName() for package and access classes that belong to class using getDelcaredClasses().
I do not know what do you really need, so I'd recommend you to explain this in separate question. probably you will receive better solutions.
However if you really need this you have to do the following:
Get your classpath by calling System.getProperty(java.class.path)
split this property to its elements by colon
iterate over the list and read each resource. If resource is jar you can use ZipInputStream, if it is a directory use File class.
filter list of resources you got at #3.
Fortunately you can use 3rd party library named Reflections that helps you to do all this without writing code.

How to use ROME for RSS

I am using the code from Rome's tutorials page http://wiki.java.net/twiki/bin/view/Javawsxml/Rome05TutorialFeedReader .
When I try to compile, it says class FeedReader is public, should be declared in a file named FeedReader.java.
I am new to Java, but I think that the FeedReader class should be part of the package used in the example, or in one of the import paths. I can't find file com.sun.syndication.samples (which is the package from the example) in the Rome library I downloaded. Any thoughts?
The code from your tutorial is
package com.sun.syndication.samples;
public class FeedReader {
...
}
It must be in a file named FeedReader.java and put in a directory com/sun/syndication/samples. If you change the name of the class, you must change also the name of the java file. If you change the package declaration, you must also change the location of the file.

Categories