Unresolved import of external library with properties file in classpath - java

I'm working with usb4java at the high level following this demo: http://usb4java.org/quickstart/javax-usb.html. My issue is that we need javax.usb.properties in the classpath, so I've put it in the src directory. I've also loaded up the .jars into a lib directory and added them to the referenced libraries. After all of this when importing "javax.usb.*" I'm getting an import cannot be resolved error and none of the types are accepted.
Note: the properties file is populated with: "javax.usb.services = org.usb4java.javax.Services" as instructed.
Thoughts?

I think you need to add to the CLASSPATH also the JSR080 (javax.usb API). Where the javax.usb interface is defined. You can download from here: http://javax-usb.sourceforge.net/
I have checked and it looks the javax.usb API is included under lib folder in the usb4java-javax distribution: http://nexus.ailis.de/content/groups/public/org/usb4java/usb4java-javax/1.2.0/usb4java-javax-1.2.0.zip it is called usb-api.jar
Then make sure the javax.usb.properties file is on the root of your application CLASSPATH.

Related

How import a file in a jar?

I have this project that it has this structure
Home
graphics
field.txt
example.java
I need to load field.txt in my example.java in jar and I use:
getClass().getClassLoader().getResource("field.txt").toUri();
but this code it give me "Null Pointer exception" .Anyone can help me?
example.class.getResource(“/graphics/field.txt“);
The class should belong to the same jar. For Class.getResource a relative “field.txt“ is possible (same package). With ClassLoader an absolute path for all classpaths is sought: “graphics/field.txt“.
To immediately read (never write) use getResourceAsStream or the URI of the getResource. One can use a Path on the URI. Files.copy(...).
One cannot at least should not) write a resource file (as it can reside in a jar jar:file://...; and jar might even be sealed; and resources might be cached by the jvm). Keep it as read-only template. Never File.
One technique is to create an application named directory in System.getProperty("user.home") and copy the template there.
To read the file it must be in classpath, you can put the file in the folder containing .class files or add it to the classpath with java -cp option.
The issue is not so much your code, but how you build and package your jar file. You will have to clarify how you are currently building your jar (using ant, maven, eclipse, etc ?).
Many articles will also advise you to separate out your resources from your source code (.java), and many IDE will support this separation direclty by allowing you to mark a folder as a resource folder. Even maven will allow you to customize this.
See following articles:
How to package resources in Jar properly
Using maven and netbeans, it is real simple: https://coderwall.com/p/d_cvrq/how-to-package-non-java-code-files-resources-in-a-jar-with-maven, or
use maven to pack javascript files in Jar?

The import com.example cannot be resolved

I am trying to compile this example given by Google on the protocol buffers:
https://developers.google.com/protocol-buffers/docs/javatutorial
It comes with a ListPeople.java and AddPerson.java file along with a bunch of imports. The problem is that i am getting "The import com.example cannot be resolved" as seen in this screenshot:
http://postimg.org/image/67whg6a57/full/
This is the full path of the import com.example java file:
http://postimg.org/image/wexoc4sez/full/
and where all of my files are located:
http://postimg.org/image/4veseacpn/full/
I've tried to do the following:
Project->Clean
File->Refresh
Property->Java build path->add external JAR:
http://postimg.org/image/xjrqhievv/full/
None of these has work. What is the problem?
So this seems to be code that is missing from your project. If you press Shift-Ctrl-T and type in AddressBook, is it there?
If it is not there then it has not been generated from the example .proto files as specifiied in the Google on the protocol buffers files
You're trying to add Java source files as if they're libraries - they're not.
Add the "src" directory as a Source Path (leftmost tab in the Java Build Path settings) instead. Or if that's already a source path, try refreshing it in package explorer. Either way, you definitely don't want to have source files as libraries...

import org.apache.axis.message.MessageElement not found? which jar to import foe this?

I have opened the jws file of StatelessAuthenticationSSOToken, sample code availabe in Oracle Repository.But even after importing all the libraries it says "Import org.apache.axis.message.* not found".I already have axis.jar in my classpath beside other Apache axis1.4 libraries.
Whic other library or jars are required to be imported
If you have the jars on your classpath, check the order. You might have to change it under the Order & Export tab.

How to use a class from JAR in eclipse

I have two jar files - jar1 and jar2. Both of them are located in C:\Eclipse projects\ and I have added the paths to both of them to the Environment Variable CLASSPATH as follows
.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Eclipse projects\stdlib.jar;C:\Eclipse projects\algs4.jar
the ".;" at the beginning were there so I left them. Then I added the jars to the project from their location C:\Eclipse projects\ and they showed up as Referenced Libraries. However, when I try to instantiate a class from the jars it does not recognize it. I am also not able to import the jar (import jar1).
After I tried adding a lib folder in the project and I added the jars there. After, I added them as references once again (so not they appear twice in the Referenced Libraries), however, I am still not able to use the inner classes. Any help will be much appreciated.
UPDATE:
Something must be wrong on my end. None of the suggestions worked for me. Here is a video with all the steps: screencast.com/t/gC81YzCsLY0e
RESOLUTION In my project I had a package called TestProject and it seems that those jars needed a default package. After deleting the TestProject package and using a defaultPackage everything worked correctly after adding external JARs as explained below.
I've got the same problem as you today, And no answer from the web can solve it. However, I fixed it at last.
In fact, there is nothing wrong with the setup, it is right to import those jars through "Add External JARs". The real problem is the location/package of you java code. I found that you have to put your .java file in the default package. For example, you will get errors if you put your java code in a package like com.xxx.yyy.ccc, below is an image which shows the right location/package you should use(see WTF.java). After doing that, you program will be able to run.
However, that is how i fixed my problem, i'm not sure that could work for everyone..
In eclipse, right click on a project->Propeties->Java Build Path->Add External JARs (Add JARs if the jar is inside the project's folder) and then choose your jar file.
From now you can use the inner classes of the jars you added. Eclipse will import them when you'll start using them.
Why don't you use these two JARs—— stdlib-package.jar and algs4-package.jar.
And below the code page(http://algs4.cs.princeton.edu/code/)
Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar or algs4.jar. Why not?
A. The libraries in stdlib.jar and algs4.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 these package versions: stdlib-package.jar and algs4-package.jar.
Warning: if you are taking Princeton COS 226 or Coursera, Algorithms, Part I or II, you must use the default package verison of our libraries to facilitate grading.
Showing my test success:
If you have a folder with your JAR files into the project:
Right click on the project>Build Path>Configure Build Path;
At the tab "Libraries" click on Add JARs, search and select the JARs files you want to use.
If you have yours JAR files any other place outside the project:
Right click on the project>Build Path>Configure Build Path;
At the tab "Libraries" click on Add External JARs, search and select the JARs files you want to use.

Accessing XMLBeans generated schemas.jar in Maven project

I have a schemas.jar supplied to me and am trying to access it from within a simple Maven project. I have put the schemas.jar file in my src/main/resources directory, which is in my classpath.
When I am trying to access the created documents with something like:
GetOrdersRequestDocument getOrdersRequestDocument = GetOrdersRequestDocument.Factory.newInstance();
It complains about the GetOrdersRequestDocument (can't find it).
How do I get the project to pick up these classes? Do I need to import anything specific?
I have put the schemas.jar file in my src/main/resources directory, which is in my classpath.
Yes, the files in src/main/resources directory are on your classpath. But this doesn't mean that the content of the jar itself is directly available. You could use a URLClassLoader to load the JAR though.
But... this is not how I would do things. If this is an option, I would just install the JAR in your corporate or local repository (using install:install-file) and declare it as a dependency. This would make the content of your JAR available to your code, like any other dependency.

Categories