Java Quarkus running a command line runner without main method - java

Hello I'm trying to follow the example here https://quarkus.io/blog/introducing-command-mode/
So I wrote a simple class with Java 11
#QuarkusMain
public class CommandRunner implements QuarkusApplication {
#Override
public int run(String... args) throws Exception {
System.out.println("hello command line runner");
return 0;
}
}
I build this with mvn clean package to get the .jar file
i then run java myjar.jar and i get the error
Error: Could not find or load main class
I thought Quarkus would generate a main method if I didn't provide one?

You have to run it with:
java -jar target/quarkus-app/quarkus-run.jar

Related

How to launch program in psvm?

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

No junit results (pass OR fail) displayed in terminal

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

Trouble running java app from cmd

I have two files, app.java and test.java
They both reside in the same package, and they compile just fine with "javac app.java test.java"
Two class files are then created.
However, when I go to run them with the command "java app" because app has the main method, I get "Error: Could not find or load main class app"
app.java:
package working_directory;
public class app {
public app() {
}
public static void main(String [] args) {
test testing = new test();
System.out.println(testing.calculate(60));
}
}
This Is the test.java
package working_directory;
public class test {
public test() {
}
public int calculate(int x) {
return (int) x * x * x;
}
}
Make sure to choose the right path for compilation and running code:
D:\
+--Folder(start cmd here)
+---working_directory
+----app.java
+----test.java
How to compile
D:\Folder\>javac working_directory\*.java
How to run
D:\Folder\>java working_directory.app
To use the java command, you must specify the fully qualified name of the class you want run. This means that you need to specify the package name as well.
You should run this:
java working_directory.app
Since working_directory is the package name.
You must provide a classpath, when running it from command line:
(for windows)
java -classpath . app
You have a package name declared in other words that's a folder. Your project should look like this then
C:\YourProject
C:\YourProject\working_directory
C:\YourProject\working_directory\app.java
Your Project starts at root level so it's C:\YourProject there you have to use the command line and type java working_directory.app

Shell scripting

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.

`java CloseSignals.class` on the command-line doesn't work

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

Categories

Resources