I need to run a single test case run through the cli. I created a runner class
public class Runner {
public static void main(String ... args) throws ClassNotFoundException {
String[] classAndMethod = args[0].split("#");
Request request = Request.method(Class.forName(classAndMethod[0]),
classAndMethod[1]);
Result result = new JUnitCore().run(request);
System.exit(result.wasSuccessful() ? 0 : 1);
}
}
using this answer
Run single test from a JUnit class using command-line.
I downloaded from github a project (https://github.com/apache/incubator-dubbo) on which I want to run the single method, I positioned myself from terminal on the directory containing the Runner class I created and I launched the following command:
java -cp /path/incubator-dubbo/dubbo-cluster /usr/share/java/junit4-4.12.jar /pathClassRunner/src/com/company/Runner org.apache.dubbo.rpc.cluster.StickyTest#testHeartbeat
but I got the following error:
Error: Could not find or load main class pathClassRunner.src.com.company.Runner
can someone help me?
thanks
The command expects you to include any paths in the classpath with NO spaces. A space means that the arguments for the classpath has ended. It there is a space in one of your paths you may include double quotes:
java -cp path1:path2:path3 classname argument
java -cp "path1:path2:path3" classname argument
If a path starts with / it's a full path. If not it's relative to where you run your command. The classname must be the name of the class, not the file, so that means no .class. If your class specifies any package, then the classname is required to contain the package name too, and the path is to the root of the package, not the class itself.
Check that your user has proper access to the files, then try:
java -cp /path/incubator-dubbo/dubbo-cluster:/usr/share/java/junit4-4.12.jar:/pathClassRunner/src/com/company Runner org.apache.dubbo.rpc.cluster.StickyTest#testHeartbeat
I'm assuming your Runner is in the default package. In case it's in the com.company package, you'll want to run this instead:
java -cp /path/incubator-dubbo/dubbo-cluster:/usr/share/java/junit4-4.12.jar:/pathClassRunner/src com.company.Runner org.apache.dubbo.rpc.cluster.StickyTest#testHeartbeat
Another assumption is that your Runner.class is in your /pathClassRunner/src... directory.
Related
I wrote the following piece of code in sublime text on my macbook pro.
I saved the file inside the java folder on my desktop. When I tried to compile the program and tried to execute it, I am getting the fallowing error message " Error: Could not find or load main class Animals ".
Could someone help me in compiling and running this program
package forest;
class Animals{
public static void main(String[] args)
{
Animals s = new Animals();
Sring[] s2 = s.getAllAnimals();
}
public String[] getAllAnimals()
{
String[] s1= {"Lion", "Elephant", "Tiger","Deer","Wolf","Dinosar"};
return s1;
}
}
Make your class Animal as public and rename the file to Animal.java
Your class is in a package called forest so you need to move Animals.java into a directory called forest.
You hava a typo on line 7: Sring[] s2 = should be String[] s2 =.
Compile from the parent directory of forest. Something like this should work
$ javac forest/Animals.java
Run from the parent dir:
$ java forest.Aminals
Your program will have no output when you run it.
Look on your code there is no sring type class in java so change it to String
Sring[] s2 = s.getAllAnimals();
change it to
String[] s2 = s.getAllAnimals();
Everything will be good
try this tutorial Java and the Mac OS X Terminal and Make your class public and also make sure that the file name that contains your source code and the name of your main class is same, your main class is the one that contains main method.
First of all you can name your java file with any name.
Say Sample.java. and let us consider this file is present in D:\work
Go to D:\work folder in cmd prompt.
Keep this in mind :
If you write package forest, then your class has to be public.
So write public class Animals{}
While compiling do this javac -d Sample.java
By doing this the compiler will create the folder "forest" and inside that it will create the Animals.class.
So now in D:\work we have Sample.java file and "forest" folder
Now in your command prompt DO NOT go inside the "forest" folder.
Stay in work folder. D:\work
And Run this command from D:\work java forest.Animals
Explantion::
The name of your class that contains Main method is not Animals. It is forest.Animals This is the fully qualified name of the class. This happened becasue you put a package to it.
So while runnning you must run with fully qualified name
java forest.Animals
The name of your class is not Sample.java. <-- This is just a text file name it anything the complier will not generate a class with name Sample.class because inside this file there is no such class, the class name is Animals. So Animals.class will be generated.
And javac -d Sample.java helps you create the folder structure automatically based on the package.
For example if your class was like this
package com.stackoverflow.samples
public class Animals{
}
And you do java -d Sample.java
The Compiler will create a folder com inside that another folder stackoverflow inside that another folder samples and inside that a file Animals.class
To run this you must run it from outside the the "com" folder with fully qualified name
java com.stackoverflow.samples.Animals
For some reason, I can't run ANY programs that begin with a package declaration.
Let's say I am trying to run a simple program called 'HelloDate.java'.
package Test;
import java.util.*;
public class HelloDate {
public static void main(String [] args) {
System.out.println("Hello, it's: ");
System.out.println(new Date());
}
}
Both HelloDate.java and HelloDate.class are located in the same folder:
/Users/eduarddedu/Desktop/Test
I am trying to run HelloDate from inside the 'Test' folder; 'pwd' returns:
/Users/eduarddedu/Desktop/Test
The CLASSPATH variable is not set to anything: echo $CLASSPATH returns an empty line.
To my mind, I should now be able to run the program with the command:
java HelloDate
But all I get is this: Error: Could not find or load main class HelloDate
I have also tried setting the CLASSPATH to (alternatively) :
/Users/eduarddedu/Desktop
/Users/eduarddedu/Desktop/Test
Still nothing works.
If I delete the package declaration at the begining, I can run the program just fine, from inside the 'Test' folder or from anywhere else, by setting the CLASSPATH variable.
You are running the file from the wrong directory.
Go to /Users/eduarddedu/Desktop and run:
javac Test/HelloDate.java
java Test.HelloDate
You should call java Test.HelloDate from outside the Test folder.
In a java file I am calling command line statement to execute another java file. This is what I am doing:
List<String> paramsExecute = new ArrayList<String>();
paramsExecute.add("java");
paramsExecute.add("-cp");
paramsExecute.add("input\programs\User_K Program1");
paramsExecute.add("1 2 3");
ProcessBuilder builderExecute = new ProcessBuilder(paramsExecute);
builderExecute.redirectOutput(new File(a.txt));
builderExecute.redirectError(new File(b.txt));
Execution of one of the Java files is producing b.txt as:
Error: Could not find or load main class 1 2 3
Another java file is producing b.txt as:
Usage: java [-options] class [args...] ...
But, when I am running these statements directly from the command line, it is executing correctly. The folder input\programs\ is in the same path as the src folder. The src folder contains the Java file containing the ProcessBuilder program. I have verified that the .class file is getting created correctly and in the right folder. I am running the program in windows.
Any help appreciated!!
This paramsExecute.add("input\programs\User_K Program1"); is been treated as a single command/parameter, saying that the class path should be equal to input\programs\User_K Program1
I think you want to use something more like...
paramsExecute.add("input\programs\User_K");
paramsExecute.add("Program1");
You should specify the classpath after '-cp' like
List<String> params = new ArrayList<String>();
params.add("java"); /* name of program to run, java */
params.add("-cp"); /* -cp */
params.add(System.getProperty("java.class.path")); /* class path information */
params.add("pkg.to.yourclass.ClassToRun"); /* full quailified class name */
params.add("1"); params.add("2"); params.add("3"); /* this is parameter to main */
"input\programs\User_K Program1" in your code is treated as a classpath information, not class to run because it follows '-cp', and "1 2 3" as a class name, not arguments passed to the main method.
It is not easy to retrieve classpath from the scatch.
If you want to create a process using an class located in the sample src folder, It is good to use System.getProperty("java.class.path"); to inherite classpath, or You should type the path info manually.
I am using enthuware to practice mock questions for classpath and packages. Here is the question.
//in file ./Foo.java
public class Foo {
public static void main(String[] args) {
System.out.println("In Foo");
}
}
//in file ./com/Foo.java
package com;
public class Foo {
public static void main(String[] args) {
System.out.println("In com.Foo");
}
}
Which of the given statements are correct assuming that the command lines are executed from the current directory with default classpath set to ./classes?
The options given are
Executing java -classpath .:./com Foo will print "In com.Foo"
Executing java -classpath ./com:. Foo will print "In com.Foo"
Executing java Foo will print "In com.Foo"
java -classpath . com.Foo will not execute.
Executing java -classpath ./com:. com.Foo will print "In com.Foo"
Correct option given is option-5. The strange problem is when i try to execute option-5 from my command line it gives me the following error.
Can someone tell me what is wrong? I am not able to figure out the reason. Plus what does this
./com classpath mean?
Command Change
I noticed one strange thing, if i change the classpath and run the command as
java -classpath . com.Foo
It states in com.Foo. As soon as i change the command to add this path ./com. It gives the above mentioned error.
Thanks.
The "./com" is a relative path to the current path.
If you have your class in [current_path]/com/Foo.class
You can run your class with the following command:
java -classpath ./com com.Foo
If you are in the com folder [current_path: com]/Foo.class you can execute the following command:
java -classpath . com.Foo
I created the project in NetBeans (according to the book P. Noutona, G. Shildta "Java2. The fullest management"), having specified as an Example2 project name.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package example2;
/**
*
* #author Asus
*/
/*
* Другой короткий пример.
* Файл "Example2.java"
*/
public class Example2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int num; //объявляет переменную с именем num
num=100; //присваивает num значение 100
System.out.println("Значение num: "+num);
num*=2;
System.out.print("Удвоенное значение num: ");
System.out.println(num);
}
}
In case of creation of the project the class example2.Example2 with the D:\ExamplesFromBook\Example2\Example2 project folder (and project layout according to D:\ExamplesFromBook\Example2) was created. I created byte code by means of a command in command line
D:\ExamplesFromBook\Example2\Example2\src\example2>javac Example2.java
therefore the Example2.class file was created. Further I tried to launch various methods.class the file from command line among which, for example, I was such:
D:\ExamplesFromBook\Example2\Example2>java example2.Example2
therefore received an error
Error: Could not find or load main class example2.Example2
What command needs to be used and from what folder to launch?
Your problem lies within the package names. The class you are compiling is inside the package example2 and is called Example2, therefor it's full path is example2.Example2. So far so good. The problem is how java will find the class. Java will transform the package declaration in the command into a file path, in this case example2/Example2.class. Since your class is placed at Example2.class and not at example2/Example2.class, it will not be found and cause an error.
To fix this, create a subdirectory called example2 and place the class file in there.
go to directory D:\ExamplesFromBook\Example2\Example2\src\
then type java -cp D:\ExamplesFromBook\Example2\Example2\src example2.Example2
The main directory where all your class files will lie should be in classpath ( which is D:\ExamplesFromBook\Example2\Example2\src in your case ). Alternatively you can set classpath using your windows Environment variables also. Once that is done; you can run any of your class file using
java <full qualified class name>
for example
java example2.Example2
You are here
D:\ExamplesFromBook\Example2\Example2\src\example2
When you compile
javac Example2.java
the classes will appear in
D:\ExamplesFromBook\Example2\Example2\src\example2
Try dir and list them
Dir D:\ExamplesFromBook\Example2\Example2\src\example2
Now to run a class, it must be in the class path. The lookup will add fully qualified name of class (with dot replaced with / ) and search in every path. In your case
java -cp D:\ExamplesFromBook\Example2\Example2\src example2.Example
should work.
The java program will look for example2/Example2.class under all classpaths you give. See more details at wiki
Change your java compile command
To compile use
javac -d . ClassName.java
to run java class file with package name use
java packageName/className
OR
java packageName.className
Have you tried to compile your .java file to another folder, for xample to "build" as it is done usually?
When Netbeans compiles files automatically it creates the following structure:
"proj_dir"/src/"package"/"code".java
"proj_dir"/build/classes/"package"/"code".class
Try doing it like this, but manually. Then go to "build/classes" dir from command line and type:
java "package"."code"
This is general way to run compiled java-code.
In your case it has to look like:
D:\ExamplesFromBook\Example2\Example2\src\example2\Example2.java
D:\ExamplesFromBook\Example2\Example2\build\classes\example2>Example2.class
CMD commands to do it:
cd D:\ExamplesFromBook\Example2\Example2\src\example2
javac -d ..\..\build\classes Example2.java
cd D:\ExamplesFromBook\Example2\Example2\build\classes
java example2.Example2
It works with simple programs.