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
Related
I'm currently working on a Java project in which I have to open files I stored in a data directory next to src.
When I launch my program from Eclipse, to access these files: I type "data/fileName" whereas when I use console I have to type "../data/fileName".
(I couldn't manage to execute java src/Main from project directory and got the error :
Error: Could not find or load main class src.Main.java
Caused by: java.lang.ClassNotFoundException: src.Main.java )
Is there a way to make my program runnable on both console and Eclipse?
To give some context: I usually intended to run only on Eclipse but I encountered issues launching nano from Eclipse ($TERM variable , redirecting pipes to dev/tty also) so console execution has become a requirement.
I can't change my Eclipse configuration since this project won't be run on my computer and if it doesn't run because of default Eclipse settings, well...
Thank you for your replies.
Cordially.
Yes. Don't put those files there. There are 2 broad categories:
Read-only data that is as much a part of your app as your class files are. Think 'list of US states for the 'state' dropdown', 'an icon png to be shown on my user interface', 'a file containing a bunch of SQL to be used to initialize an empty database', and more.
For this, the proper way to go is YourClass.class.getResourceAsStream("foo.txt"), which will give you an input stream for reading the file "foo.txt", which is in the same place "YourClass.class" is. Even if it is in a jar file, for example.
This does mean that as part of your build, these files need to be in the jar. eclipse's limited build system does this. If you level up as a programmer and use a build system (you should be levelling up here), put them in src/main/resources and the build system should take care of it.
Or, the second category: Files that the user is supposed to edit, or files that need to be modified by your app.
These don't go anywhere near your jar file; executable and user-editable stuff should be nowhere near each other. These should be in the user home dir, you can fetch it with System.getProperty("user.home").
Note that to run java stuff, 'src' shouldn't be part of the story at all, java can't run source files, only class files. Also, the argument isn't a dir, it's a class name. java -cp path/to/your/app.jar com.foo.pkg.YourClass is how you run java code, not java src/Main.
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.
While testing a program I am developing that writes to a SQLite database, I noticed that if I moved my sqlite.jar file to a sub-directory, Class.forName("org.sqlite.JDBC") throws the ClassNotFoundException. Is there a way that I can change the directory that Class.forName("org.sqlite.JDBC") is looking in so that I can store the file where I want it?
It looks like the classpath is set to work with that location. If you are using an IDE, add the library to your build path in your project properties and then execute your code. The IDE will do the rest of the things you need to do.
If you are working with classpath setup, you should not move your files from the classpath where the class files are specified and your program will look for its desired class to load.
I have made a Java applet game in Eclipse which has many classes and media associated with it. I have now been trying to finally test the game in a browser but I am having a hell of a hard time getting it to work.
I have exported a .jar file (a non-runnable, could that be a problem?) and tried many different ways of loading the applet. I have read over the materials on the oracle website as well.
My first two basic questions are:
My applet does not have a static void main(String args[]){ line because I was under the impression that for applets you use a init() and start() method. Could this be the problem?
if not, my class which contains the init and start and the other basic methods is called Start.class and is located in a bin/ directory. Am I able to edit the manifest which is included in the .jar exported from eclipse and but this Start.class as the main class using Main-Class: Game.Start?
Another very basic question also when it comes to directories in java or specifically in .jar archives, are folders in a path names always separated by a .? or do /'s work too?
My project name is simply Game, I have a src folder with .java files and a bin folder with .class how do I direct the manifest to the bin/start.class file?
Sorry this has been rather frustrating especially because I really want to be able to share this applet. Any help would be greatly appreciated.
1) My applet does not have a static void main (args[]){ line because I was under the impression that for applets you use a init() and start() method. Could this be the problem?
No, this is not a problem, as Applets and JApplets do not use main methods to run. Note that some may have main methods that may be used to allow the coder to test the code in a non-applet environment, but when run as an applet, the main methods are ignored.
2) Another very basic question also when it comes to directories in Java or specifically in .jar archives, are folders in a path names always separated by a "."? or do "/"'s work too?
the directeries in jar files use "/". Please check that you are not trying to use resources as Files since jar files do not hold files (but rather resources).
My project name is simply Game, I have a src folder with .java files and a bin folder with .class how do I direct the manifest to the bin/start.class file?
Consider showing the structure of your jar file and also a small test html file where you try to run the applet.
Start.class and is located in a bin/ directory
I agree with Hovercratft about a missing main (not missing), about runnable jar (not necessary).
One problem which might exists is the exact location of files and naming of classes.
If your class name is bin.Start, because you defined a package bin (very uncommon), you need to put the bin directory into the jar.
If you didn't declare a package, you don't have a directory to put into the jar. Maybe eclipse handles this for you automatically.
To test your applet, you need a html file, and start the html file in the appletviewer. If this works, you test it in the browser. Else verify that you can start foreign applets in your browser, to make sure it is installed in the correct way.
Since you show a sloppy habit in the question of cases: Start.class or start.class: This is significant for Java. Use initial uppercase names everywhere.
I'm adding a Junit test to one of my classes using NetBeans 6.7.1, I need to be able to load a an xml file specific to testing, so I've included it in the "Test Packages" folder (along with my actual test). I'm running into 2 issues,
(1) getResource is looking in the wrong directory
(2) my xml test file doesn't get copied when I run tests (note, this functionality works with I add files to the "Sources Packages" directory).
In my test class:
this.getClass().getResource("/")
returns:
D:\UserProfiles\myName\My Documents\NetBeansProjects\ProjectExample\build\classes
I need it too return:
D:\UserProfiles\myName\My Documents\NetBeansProjects\ProjectExample\build\test\classes
(As that is where the test classes are being compiled)
It seems rather hacky calling getResource, getting the parent, and then looking in test\classes. Is this the proper way of getting the path to my test file ? maybe this is a bug in netbeans ?
Also, when I right click on my testFile and "run tests" , only my test class files get copied to the test/classes directory and not my xml test file. How do I tell Netbeans to make sure to copy a regular xml file along with class files to the build directory.
I would also like to avoid hacking the ant build to copy my test files.
I recently ran into this problem using NetBeans 6.9.1. Here's how I solved it.
Open Project->Properties->Libraries
Select the Run Tests Tab
Click on Add Jar/Folder
Navigate to where you've stored the resource files
Add the folder
Now running tests using those resources will work. I tested this with NetBeans 6.9.1 and a simple Java Application.
NetBeans creates resource files in the src directory by default (default package), so I added the src folder in step 5 above. Once you do that tests looking for a resource file in the classpath will find it.
Since NetBeans packages the resources found in src folder by default, you don't need to copy the files around and keep them in sync.
If you want test resources different from production resources, you can add the test resources in the default package under the test folder. Then instead of adding the src folder in step 4/5 above, add the test folder.
Just put your file in the same package as your test, say, data.xml in package foo.bar. No copying or build script hacking is necessary, just refer to the file like this:
getClass().getResource("data.xml");
Alternatively, you can do this:
getClass().getResource("/foo/bar/data.xml");