java gui testing - java

in the java test I have :
package Tester.GUI.api
public class Test1{-----}
in the ".bat" :
<path to java> -classpath<all jar defined in the classpath separated by ";"> org.junit.runner.JUnitCore Tester.GUI.api.Test1
when I launch th ".bat" I have the following :
JUnit version 4.6
Could not find class: Tester.GUI.api.Test1
Time: 0,203
OK (0 tests)
I have verified jar files , typo but not found the cause
someone could help please?

The -classpath option needs to take a set of directies and/or jar files. Currently those are missing, so your main class (JUnitCore) is being used as the classpath, and Test1 used as the main class. I'm guessing you want JUnitCore as the main class and Test1 as the argument.
Try something like this (substituting the actual names of the jars you're using for jar-file1, jar-file2):
java -classpath <jar-file1.jar>;<jar-file2.jar> org.junit.runner.JUnitCore Tester.GUI.api.Test1
Edit:
Assuming jar files are specified correctly, the error message indicates that the Test1 class isn't on the classpath. If you're using an IDE, you need to find the output directory for compilation (for example, in Eclipse it is <project>/bin by default). Within this directory you will find other directories with the structure Tester/GUI/api. A file called Test1.class will be in the api directory. It is this output directory that needs to be added to the classpath (that is, the one above the Tester/GUI/api structure).

It says that the class that it cannot find is Tester.GUI.api.Test1. What directory are you running the test from, and what directory is your Test1 code in?
If you set the -classpath variable, you may also need to include the current directory in your classpath. So, if your original command was
java -classpath C:\foo.jar;D:\bar1\bar2.jar org.junit.runner.JUnitCore Tester.GUI.api.Test1
you could add the current directory to your classpath by adding ;. to the end of your -classpath option like this:
java -classpath C:\foo.jar;D:\bar1\bar2.jar;. org.junit.runner.JUnitCore Tester.GUI.api.Test1
This will work as long as the Test1 class is in the Tester\GUI\api directory under the current directory.
The tricky thing with setting the classpath in Java is that you don't want to give the directory where your actual .java files are, but instead you want to give the directory under which you can find the directories that are named for the packages in your code.
For example, if I compiled a class Foo in a package bar.baz, then I should have a file named Foo.class in a directory named baz inside another directory named bar. If I want to include Foo in my classpath, and if Foo.class is located at
C:\Users\Joe\Code\bar\baz\Foo.class, then I have to either run
java -classpath C:\Users\Joe\Code [main class goes here]
or else I have to change my directory to C:\Users\Joe\Code and run
java -classpath . [main class goes here]

Related

Compiling Java programs in command line

Do I have to run set Class and Set ClassPath in Commandline every time for compiling Java programs with these commands
set class ="C:\Program Files\Java\jdk1.8.0_121\bin";
set classpath="C:\Program Files\Java\jre1.8.0_121\lib\rt.jar";
C:\>Javac Test1.java
C:\>Java Test1
even I added up folder up to bin in environment variable for setting up the environment variable.If I do not run SET CLASS and SET CLASSPATH, I get an error Error: Could not find or load main class Test1
Please answer thanks.
What i would do is :
1) Add jdk path to PATH.
2) Create a folder with all my classes in , the cd to that folder "javac" every class and then "java" the one containing the "main" method.
You also need the path where your compiled class files are in your classpath!
You only included the tools.jar - but not your class file(s).
When compiling, you should only point to those JVM classes, but when running your application, your classes need to be found too!

Turn multiple folders with Java code into runnable jar file

I have some Java code on my machine that I run from the command line. I want to create a runnable .jar file from this code so that I can distribute my application more easily.
The code is in four folders, called fol_a, fol_b, fol_c, and fol_d. Each of these contains a /bin subfolder, containing the .class files, and two of them (fol_a and fol_b) also contain a /lib folder, containing some .jar files that the code needs.
fol_d contains the class to run, Demo, which is in a package called machineLearning. The full path to the class is
fol_d/bin/machineLearning/Demo
I currently run the code from the command line as follows:
$ cd fol_d/bin
$ java -cp ".:../../fol_a/bin:../../fol_a/lib/*:../../fol_b/bin:../../fol_b/lib/*:../../fol_c/bin" machineLearning.Demo <param_1> <param_2> ... <param_5>
where <param_1> to <param_5> are the arguments given to the Main method in Demo.
What I want is to create one single .jar file that contains all the code that is necessary to execute Demo successfully, i.e., the code in fol_a through fol_d. I then want to be able to run this .jar file from the command line, giving it the arguments that go to the Main method in Demo. Something like this:
$ java -jar MyApplication.jar <param_1> ... <param_5>
Is this possible? How would I do this? I've been trying to find an answer online, but the amount of information confuses me.
UPDATE
Right! So it seems that all I needed to do was this:
copy the contents of the bin directories to a new dir myapp
make a manifest.txt file that specifies the main class to run, as well as the classpath
jar myapp: $ jar cmf manifest.txt myapp.jar -C myapp/ .
execute the jar: $ java -jar myapp.jar <arg_1> <arg_2> ... <arg_n>
Yes it is possible.
Use "cp -R" to copy all 4 folders' bin directories into one directory ... preserving the subdirectory structures. (Read man cp if you don't understand how. Install the manual entries if they are not installed.)
Use the jar command to create the JAR file from the consolidated directory.
UPDATE
When you create the JAR file, the paths within the JAR (i.e. in the JAR file index) must match the respective classes fully qualified names.
If you are creating an executable JAR, the Main Class attribute must specify the fully qualified class name.
If you misname the JAR file entries then either java won't find the classes, or it will refuse to load them because the pathname and classname don't match.
These requirements apply for all JARs, but from your comments it seems that you have overlooked this.
In your comment, you seem to have used the wrong classname in the Main Class attribute ... unless you declared the class in the fol_d.bin package!
UPDATE 2
Here is an example to illustrate my point about fully qualified classnames
package foo.bar;
public class Main {
...
}
The simple class name is Main. The fully qualified classname is foo.bar.Main. If you put the ".class" file for this class into a JAR, the pathname in the JAR file for the class must be:
/foo/bar/Main.class
The package name (foo.bar) maps to the directory path in the JAR file index; i.e. "/foo/bar".
If the pathname in the JAR file isn't that, then the classloader won't find it.

Java can't find main class

I have written the following Java source file (Hello.java):
package com;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello!");
}
}
I save this to C:/tmpjava/Hello.java.
From the command line, I navigate to that directory and run javac Hello.java. Then I run dir:
Hello.class
Hello.java
Then, from the same directory that I just ran javac from, I run java Hello.class and get:
Exception in thread "main" java.lang.NoClassDefFoundError: Hello/class
Caused by: java.lang.ClassNotFoundException: Hello.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Hello.class. Program will exit.
What's going on here?!? How can javac run fine, but not java?
Your class Hello belongs to the package com. So the fully qualified name of your class is com.Hello. When you invoke a program using java on the command-line, you should supply the fully-qualified class name of the class that contains your main method and omit the .class, like so:
java com.Hello
The java program needs this fully-qualified class name to understand which class you are referring to.
But you have another problem. The java program locates packages, sub-packages, and the classes that belong to them using the filesystem. So if you have a package structure like com.Hello, the java program expects to find a class file named Hello.class in a directory named com, like this: com/Hello.class. In fact you can observe this behavior in the Exception that you see; you've incorrectly used Hello.class, which java is interpreting as a package named Hello, and a class named class, and is looking for the directory structure Hello/class:
java.lang.NoClassDefFoundError: Hello/class
But the compiler javac doesn't set up this directory structure by default. See the documentation for javac, but the important bit is this: when you do your compiles, you can specify a destination directory using the -d flag:
-d directory
Set the destination directory for class files. The destination directory must already exist; javac will not create the destination directory. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d c:\myclasses and the class is called com.mypackage.MyClass, then the class file is called c:\myclasses\com\mypackage\MyClass.class.
If -d is not specified, javac puts the class file in the same directory as the source file.
The last bit in bold is the source of much confusion for beginners, and is part of your own problem.
So you have two alternatives:
In your case, it's fine if you supply the current directory as the destination directory, like so (the period . means current directory):
javac -d . Hello.java
If you invoke the compiler like this, it will create the com directory for you, and put your compiled class file in it, the way that the java program expects to find it. Then when you run java as above, from c:\tmpJava, your program should execute.
You could set up your source code using a directory structure that mirrors your package structure: put your source file Hello.java inside a directory called com, in your case: c:\tmpJava\com\Hello.java. Now, from c:\tmpJava you can run your javac compile like this:
javac com\Hello.java
You haven't supplied the -d flag, but that's fine, because you've created the directory structure yourself, and quoting again from the documentation above:
If -d is not specified, javac puts the class file in the same directory as the source file.
Again, when you run java as above, your program should execute.
Note that this second alternative is one that is commonly employed by java programmers: the source code files are organized in a directory structure that mirrors the package structure.
In this explanation we've ignored the concept of the classpath. You'll also need to understand that to write java programs, but in your case of simply compiling a program in the current directory - if you follow one of the two alternatives above when compiling your class - you can get away without setting a classpath because, by default, the java program has the current directory as a classpath. Another quote, this one from the documentation for java:
-cp classpath
Specify a list of directories, JAR archives, and ZIP archives to search for class files. Class path entries are separated by semicolons (;). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.
If -classpath and -cp are not used and CLASSPATH is not set, the user class path consists of the current directory (.).
Note that when you use an IDE like Eclipse to run your java code, this is mostly handled for you, but you'll still run into classpath issues.
The syntax of the Java command is:
java [classname]
not
java [filename]
Java looks in its classpath for a class of the name you have provided. Usually the classpath includes the current directory, so:
java Hello
... will find Hello.class in the current directory, and run its main() method.
However, if the class is somewhere else (like in a .jar, or somewhere else in the filesystem) you can specify it with the CLASSPATH environment variable, or on the commandline:
java -cp build/classes Hello
java -cp build/jars/myjar.jar Hello
The class should be in C:\tmpjava\com\Hello.class
And you should run from C:\tmpjava: java -cp . com.Hello
When you put a class in a package it defines the file structure of the class. I.e. your class which in package com should be in folder com
Run the following command
java -cp . com.Hello
java -classpath c:\tmpjava com.Hello

Error when executing a JAR file

I've been learning about JAR files and wanted to try and create and run one myself. I carried out the following steps:
Created a project folder with a 'source' subfolder and a 'classes' subfolder
I wrote 2 source files, one with a main method which creates an instance of the other class and runs a simple method in it.
Compiled these to the 'classes' subfolder. I checked to see if they would run. They did
I created a manifest.txt file and filled in the Main-Class: xxxx and hit the return key. I saved this in the sources subfolder
Created a jar file in the classes subfolder by writing
jar -cvmf manifest.txt zzz.jar *.class
Tried to execute the jar file by typing
java -jar zzz.jar
This gives a ClassNotFound exception. If I try to execute the jar by double clicking on it in windows I get an errorbox saying "Could not find the main class xxxx"
I've double checked the spelling of the class inside the manifest file and it's correct.
Possibly important: I have to compile my programs using java -cp . xyz as there is an issue with my classpath. Does this mean that I need to execute jars in a different way as well? I tried
java -cp . -jar zzz.jar
but ended up with the same exception.
Edit: I ended up starting from scratch and now it runs (with the basic -jar zzz.jar command). Frustrating that I don't know what I was doing wrong but glad that it is working!
Shouldn't number 5. be run in the classes subfolder, where all your class files are? And if your classes are in packages, which they should be, you'll likely want to use * instead of *.class..?
To check what your jar file contains you can run:
jar tf zzz.jar
You will probably have to supply the entire path of the .class file you wish to execute after the classpath. ie java -cp xxx.jar classes.mainProgram.class. Where classes is the name of the folder which contains your class files.

Java command-line problems with .jar libraries

I have a single .java (driver.java) file I'm trying to compile and run from the command-line. It uses the external library called EXT.jar, whose structure is just a folder called EXT with a few dozen classes within it.
So I run:
javac -cp EXT.jar driver.java
This compiles the class just fine.
then when I run:
java -cp EXT.jar driver
I get a java.lang.NoClassDefFoundError.
Oddly enough, if I unpack the JAR (so now I have a folder in the root directory called EXT), the last command works just fine!! Driver will execute!
Is there any way I can make the driver.class look for the need class files from EXT.jar/EXT/*class instead of an actual EXT folder?
Thanks!
You're compiling the class to the local directory. So when you run it, you need to include the current directory in your classpath. E.g.:
java -cp .;EXT.jar driver
Or in linux:
java -cp .:EXT.jar driver
With the way you have it now, you're saying your classpath is only EXT.jar (along with whatever is in the CLASSPATH environment variable) and nothing else (which is why the current directory, where driver.class is located, is excluded)

Categories