I'm running the demo in:
https://github.com/junit-team/junit4/wiki/Getting-started
I copied Calculator and CalculatorTest exactly as displayed in the page. Compiling failed though:
D:\workspace\junit-example>java -cp d:\junit\latest\junit.jar;. CalculatorTest
Error: Main method not found in class CalculatorTest, please define the main method as:
public static void main(String[] args)
So I created this file:
public class Runner {
public Runner() {
}
public static void main(String[] args) {
CalculatorTest c = new CalculatorTest();
c.evaluatesExpression();
}
}
The problem is, even though everything compiles and runs (see output below), there isn't any output from JUnit. What is needed to see the result of the test?
D:\workspace\junit-example>javac -cp d:\junit\latest\junit.jar;. *.java
D:\workspace\junit-example>java -cp d:\junit\latest\junit.jar;. Runner
D:\workspace\junit-example>
You are running it in a wrong way.
You do not need Runner class at all. Also you need to run jUnit runner class and pass class with test methods as a parameter. This is mentioned in that tutorial:
java -cp .;junit-4.XX.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest
You don't need your Runner class. Instead, when you run your program, specify org.junit.runner.JUnitCore as the class to run, not CalculatorTest. That way, JUnit will run the test. It's mentioned further down on the page you linked to, under "Run the test."
java -cp .;junit-4.XX.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest
Related
How to launch a program in psvm with one command?
How does the application know which class to launch first?
I know that psvm should only have starting command and nothing more.
Could you explain this to me?
I mean how to create proper public static void main(String[] args) in a simple program on Maven. Should I create a class i.e. Starter with method run (with sequence actions) and in psvm write new Starter().run()?
psvm stands for public static void main as shown below:
public static void main(String[] args) {
// Your code here
}
psvm is not a standard Java terminology. You can call it as a Java slang. It is the entry point in your standalone Java application i.e. when you run an executable jar, it will execute the class having psvm. There are so much of content about it on the internet e.g. https://dzone.com/articles/executable-java-applications
The main() is the first entry point of Java application. Java Virtual Machine is told to run an application by specifying its class using the application launcher & it will look for the main() with exact syntax of public static void main(String[]).
Considering your comments you want to do something like this :
public class Starter{
public static void main(String args) {
new Starter().run();
}
public void run() {
//your logic
}
}
once you write this,
you have multiple options to run this I am mentioning a few
1) by building jar and then executing that jar using java -jar command
2) or by executing maven command once you have compiled your program using mvn compile, mvn exec:java -Dexec.mainClass="complete name of your main class i.e including package name."
a few links
http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/
https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Maven_SE/Maven.html
hope this might help
I have a robocode.jar to extend,and then i want to run it from shell.
I can't run it from shell because i get the following error
error: Main method not found in class tema1, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
I've searched ,but no answer helped me.
This is my script:
#!/bin/bash
javac -cp robocode.jar: tema1.java
java -cp robocode.jar: tema1
My class it's extendig their class and I have to make it work in the game.
http://robocode.sourceforge.net/
When I run it from Eclipse it's working fine,the game starts,but from shell it's not working
Can someone help me figure out what i have to do to make it work from shell and not only from ide?
Like the error says, you'll have to define a static method main in your class tema1 as follows:
public static void main(String[] args) {
/* your main program */
}
The main method is where your program execution starts.
I compiled my only class, CloseSignals.java, producing CloseSignals.class. When I try to run it using java CloseSignals.class, it says "Could not find or load main class CloseSignals.class". I have looked this problem up and it seems like this problem should only occur if i'm trying to start the program from some class in a package. This isn't in a package, I'm just trying to compile this simple program.
What could it be?
You have to have method public static void main(String[] args) defined in your class in order to be able to run it - this is where your program starts:
public class CloseSignals {
public static void main(String[] args) {
// your code here...
}
}
If you have the main method in that class, you can compile it like this:
javac CloseSignals.java
And then run it using the following command:
java CloseSignals
Note you run it using the class name, not the actual file name.
When you compile your code use '.java' extension like
javac YourClass.java
but when you run it Not use '.class' extension
Use like
java YourClass
I have run into an error. I think my code is right. here it is... please tell me what I have done wrong!!!
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
The error I keep getting is...
Error: Could not find or load main class javatest.java
The file name of the Java Class file must be the same one as your Class.
Class: public class HelloWorld {}
File: HelloWorld.java
The code seems right. Perhaps the issue in how you run it.
The convention is to keep the file name and the public class name as the same.
When compiling, use javac <filename>.java. Then to run simply use java <classname>. So, you would use java HelloWorld, HelloWorld being the class that contains the main method.
I have the following simple code
public class Tester {
static class TesterChild {
public static void main(String args[]) {
System.out.println("Test");
}
}
}
It compiles fine. But when I run it I get the following Error
[aniket#localhost src]$ java Tester
Error: Could not find or load main class Tester
Question is why can't we define our main method in static inner class?
Update1 :
As specified in the answers/comments I have change the code to following
public class Tester {
public static class TesterChild {
public static void main(String args[]) {
System.out.println("Test");
}
}
}
I compiled it and it made two class files Tester.class and Tester$TesterChild.class. But still i am getting error
[aniket#localhost Desktop]$ java Tester$TesterChild
Error: Could not find or load main class Test
Update 2:
Ok now I included current directory in the classpath and executed still getting error
[aniket#localhost Desktop]$ java -cp . Tester$TesterChild
Error: Main method not found in class Tester, please define the main method as:
public static void main(String[] args
It can be run as main but you are not using the right class. Your main class is not Tester but Tester.TesterChild.
In Eclipse it will run without any setup but from the command line you have to use the java 'yourpackage.Tester$TesterChild' syntax as others mentioned above.
You need to wrap the name of your class in ''s because on linux/unix the shell might think that $TesterChild is a variable. If you try it out in the prompt you will get something like this if you omit the ''s:
Error: Could not find or load main class Tester
If you need to explicitly set the classpath you can use the -cp or the -classpath option or you can set it from the commandline: set CLASSPATH=/somedir
Since you have defined main() method in inner class.
Run the inner class to get main() to be executed.
use command line java Tester$TesterChild.
update
I think you are placing java file in some package.If then use this command line.
java -cp . yourPackageName.Tester$TesterChild
For example I have placed file in a package named test.Then my command is like this
java -cp . test.Tester$TesterChild
Beware that the dollar sign has special meaning to most shells. I.e. if you write
java -cp . Tester$TesterChild your shell might replace $TesterChild with the contents of an environment variable with that name or silently replace it with nothing if it doesn’t exist. Try java -cp . 'Tester$TesterChild' or java -cp . Tester\$TesterChild