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?
Related
I need to test some code on the fly using the Eclipse Scrapbook. Is this even the best way to do it? I don't come from a Java background. Anyways, its frustrating me because I'm trying to define a simple class in Java:
public class RegistrationResponse {
public String message;
public Boolean success;
}
I want to use this class in my Scrapbook. So here's my project structure:
src
|-(default package)
| |- RegistrationResponse.java
|- Scrapbook.jpage
I went into my Scrapbook, and added the RegistrationResponse type from the Java Snippet Imports window. Then, when I run my code, it tells me this:
The import RegistrationResponse cannot be resolved
You gotta be kidding me. How do I do this? Anyways, I noticed I wasn't able to add the default package in my snippet imports. Is that why its failing? Is it supposed to be added by default or no?
Do not put classes into Default package. put it into a subpackage.
If you are using eclipse, you con use shift+ctrl+o to Import all referenced classes
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();
}
}
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 = ...
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.
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.