class.getClassLoader.getResource() returns null - java

Previously I built single jars with ant, for each application i wanted from my project and calling the application with java -jar application-name.
I have now moved to using gradle and having a single jar and call the applications with
java -cp fullpath-to-class.
Everything works as expected for all but one application where i now get a
null pointer exception trying to load the resources required.
If I move the files into the directory of the class which is looking for the files everything is good once again , but having them in a different directory seems to be problematical.
Have you any suggestions on the best approach to
A. debugging this effectivey
B. Having the file in a separate directory

Have you any suggestions on the best approach to A. debugging this effectively
Use jar -tvf ... to check that the missing resource is actually in the JAR file, and that it has the correct pathname in the JAR.file
Use a debugger, and set a breakpoint on the code that is trying to load the resource. Single step until you get to the point where you have the absolute resource path. Check it.
B. Having the file in a separate directory
Umm. I'm not sure what your actual problem is, but my guess is that it is to do with resolving relative resource pathnames. I suggest using absolute resource pathnames instead.
I guess the problem could be in the way you are building the JAR file, but you've not provided any concrete details of how you are doing that.

Related

get the root directory of a maven application

I need to provide a settings file for my program, to which the user should have access to write some of the settings i need.
I created a file under a new directory (called settings) on the root of my application, but i have problem finding it at run time.
I use
File SettingsFile=new File(ClassLoader.getSystemClassLoader().getResource(".").getPath()+"settings/CreateSettings.txt");
When i execute this under eclipse i get
/application/home/dir/target/classes/settings/ZipCreateSettings.txt which is wrong.
If i execute it on terminal using java -jar, i get the correct path,
/application/home/dir/settings/ZipCreateSettings.txt
This would cause me problems cos i need to run the application directly from eclipse and not use the terminal, even though it is going to be executed using the jar when it is up and running.
I cant keep it like that anyway, cos this code might end up in someone else's hands, and they would have no idea what to do with it.
I have also used some other techniques like
new java.io.File("").getAbsolutePath(); but this always gives me the current working directory, so if i execute the jar from /home, i would get /home.
I think the problem might be maven (which i am not familiar with at all) since my code worked with a plain java application some time ago.
Since your file will be located outside your classpath, it is basically outside your application. Your application is not aware of files existing outside it's classpath. So you will need some kind of way to provide the Full/Absolute path to your file. You can't use the classloader in your case.
I suggest you use a system param instead of a hardcoded value. See here

UnsatisfiedLinkError, but library is in path

I'm a little confused by this error. I'm new to Java and the error seems pretty self explanatory, but I've checked my paths (even defined my own) and it still fails to find this library. Is there something I'm doing wrong? See directory screenshot and error screenshot below:
UPDATE
UPDATE 2
If I create a new NetBeans project this works fine. Same code and everything. If I create new IntelliJ project I get this error above. I must be missing something in IntelliJ
UPDATE 3
Found the solution. Adding dependencies is a bit different in IntelliJ. Thanks all for the help.
http://www.jetbrains.com/idea/webhelp/configuring-module-dependencies-and-libraries.html
Obidisc4j is a .jar file. You just dont see its extension in the explorer.
Regular jar files are not loaded by System.loadLibrary. They are automatically loaded by the JVM's classLoader.
You are using a native library. It doen't matter if that library is in the classpath. There are 4 ways you can make the Java runtime load your shared library at runtime:
Call System.load to load the .so from an explicitly specified absolute path.
Copy the shared library to one of the paths already listed in java.library.path
Modify the LD_LIBRARY_PATH environment variable to include the directory where the shared library is located.
Specify the java.library.path on the command line by using the -D option.
Seems like your application is trying to find out a obidisc4j.dll (for Windows) or obidisc4j.so (for Linux). The file must be present on the PATH (but not the classpath). If you are not sure what PATH is your Java application searching in, you can write the following statement, before the point where the exception takes place, to find out the PATH.
System.out.println(System.getProperty("java.library.path"));
This will tell you about the paths where your DLL or SO file should be placed. You just need to place the file in ONE of those N-paths.

JAVA ClassLoad same class name

Yesterday i thought one question ,below is the detail:
I have 3 JAR files, a.jar, b.jar ,c.jar . both these jars files have a class named com.test.Test ,and sayHello() was defined in this class.
I create a web application, i reference a.jar,b.jar,c.jar . And in main method, i involve sayHello(); .at this time, which com.test.Test will be load?
the result is a.jar.
any body tell me the reason ?? thanks in advance!!!
That is what java language specification says. It loads what ever the class first occurs in classpath and ignores other.
Instead of focusing on which one will be loaded, realize that the stuff within the JAR files probably need their com.test.Test class instead of someone else's com.test.Test to work properly. That means for a functional system you'll have to make a way that a.jar finds a.jar's com.test.Test instead of the one in b.jar. The same goes for b.jar finding it's classes in preference to a.jar's.
The only way to do this is to use a framework which adds name spacing beyond the java package mechanism. This is typically done with multiple classloaders, often one for each JAR file. You can write such a thing yourself (Tomcat did), where you need to specify the rules for cross-loader discovery, or use something akin to a OSGi framework.
Whichever Jar File comes first in your classpath will be used..
You can modify your CLASSPATH environment variable to the path of your Jar file
Suppose you modify it as below: -
set CLASSPATH = %CLASSPATH%;.;a.jar;b.jar
then a.jar will be used..
You can also modify it by: -
set CLASSPATH = %CLASSPATH%;.;b.jar;a.jar
In this case, b.jar will be used..
These commands you need to run from your Command Line..
** NOTE: - If you are using any IDE, then they don't use System Classpath.. You need to set different classpath for the IDE you are using..
If you are using an IDE, such as eclipse, you can modify your classpath on the properties of the project, then go to Build Path, and then you have the Order and Export tab where you can move up and down the jars. The one of the top will be the first taken by your application.
This you can also do manually by editing the file called "classpath" which is on your project and move to the top the jar you want your application to use first.

Eclipse project can not reference a file using relative paths

My file is located under the src directory. However, when I try to call it using "src/readme.txt" the file is not found.
In fact, it states that java is looking for "C:\Documents and settings\john\My Documents\Downloads\eclipse-win32\eclipse\coolCarsProject\src\readme.txt".
How do I fix this? I do not want to put in the absolute path all the time.
Do I need to fix the classpath, buildpath, or change the project root, etc? It is not at all obvious from the roughly 1000 settings in Eclipse for a newbie.
First, you have to decide if you want to load the file from the file system, or if the file will in fact be bundled with your application code.
If the former, then you should really think about how your application will be launched when actually deployed, because using a relative file path means that the program should always be started from the same location: a relative path is relative to the location from where the program is started (the current directory). If this is really what you want, then edit your launch configuration in Eclipse, go to the Arguments tab, and set the working directory you want. But the src directory is not where you should put this file, since this will copy the file to the target directory, along with the classes, that will probably be put in a jar once you'll deploy the application.
If the latter, then you should not treat the file as a file, but as a resource that is loaded by the ClassLoader, using ClassLoader.getResourceAsStream() (or Class.getResourceAsStream()). Read the javadoc of those methods to understand the path to pass. If you put the file directly under src, it will be copied by Eclipse to the target directory, along with your classes, in the default package. And you should thus use SomeClass.class.getResourceAsStream("/readme.txt") to load it.
Using paths relative to the current working directory is not a good idea in general, as it's often quite hard to establish what your current working directory will be. In Eclipse, it will be your project folder (unless you set it to something different in your launch configuration), in webapps it will be the webapp's root directory, in a command line app it could be anything.
Try this one:
String filePath = ".\\userFiles\\data.json";
where «.\» is a root for the Eclipse project, «userFiles» is a folder with the user's files inside of Eclipse project. Since we are talking about Windows OS, we have to use «\» and not «/» like in Linux, but the «\» is the reserved symbol, so we have to type «\\» (double backslash) to get the desired result.

HP Service Test - Call Java Class service - having class path issues

I am currently using HP Service Test 11.10 to test a call to a web interface, but as part of this test I'm adding some data to an oracle Database, which by default HPST does not support. HPST does support calls to a java class, so I'm attempting to call a Java class in a .JAR file. I have placed this .JAR file in the directory with my test solution. When I add a step to Call Java Class I can select and run the file just fine, but I'm having a problem where I cannot move my test solution to a different directory without getting a java.lang.NoClassDefFoundError when I run my test.
As near as I can tell, it's trying to call the specific path for the .JAR when I add it (I.E. something like c:\temp{test directory}, even though I may have moved it to c:\users{user name}\workspace{test directory}). How can I change this so that it always looks in the same directory as the test solution for this .JAR file?
....
From the HP Software Solutions Community forum:
Hello, You are right, the path of the .jar file is indeed preserved
within the test as it is translated to code. This path cannot be
modified, and there's no way to add jar paths relatively to the test
path.
But still, one workaround I can think of is to include this jar in
what we call InternalJavaClasspath:
You can define a new path to that jar of yours within
InternalJavaClasspath.dat located under
{hp_service_test_installation_folder}\dat And then you must put this
jar in a subfolder under the installation folder.
The common location is: bin/java - you can create a folder here called
MySpecialJars and the respective line in the dat file will be:
bin/java/MySpecialJars/MySpecialJar01.jar

Categories