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
Related
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
When i'm trying to run in Eclipse program which has no main method as Java Application it always requires a main method to define. Could anybody please explain is there any simpler way to make configuration run without implementing
public static void main(String[] args) {
}
Every program requires a main method.
The public static void main(String args[]) starts the main thread which your program will run on.
There's more information available here.
You need a main method to run your program.
public static void main(String[] args){
YourClass newObject = new YourClass();
}
Put this method in your class (replace YourClass with your existing class) and hit the run button.
run in Eclipse program which has no main method
NO.You won't be able to run a class by using IDE like Eclipse or anything else. Even you won't be able to run your java program by using Java command from your command prompt, until your java program has public static void main(String args[]) method on it. If you try to run, you will get below mentioned error
Error: Main method not found in class Test, please define the main
method as: public static void main(String[] args)
But you should also know that, we can run a Java program without main method on managed environment like the case of Applet, Servlet and MIDlet. There are actually different types of execution model available in Java, for example Applets which run on browser doesn't have main method. Servlet is also java program, which runs in a Servlet container and since Servlet is also a Java program, we can say that it runs without main method. Third one on this category is MIDlet, which runs on mobile devices.
I want to run a specific .java file (a class), but Eclipse is not co-operating.
I have tried to follow the instructions here -->
eclipse how to run a different class
... but my "Run As" menu item never contains "Java Project".
I have tried accessing the menu after right-clicking in the .java pane and tab itself, and from the .java name and class name in the Package Explorer, but of course that doesn't make a difference. The only option I ever get is "Run Configurations".
(and yes, my .java has a "main" method.)
import com.jsyn.JSyn;
public class SuperSimpleSounds {
public static void main() {
[...]
What, exactly, is needed to be able to run an individual class (an individual .java file)?
Add a String array argument to the main method as expected by the JVM
public static void main(String[] args) {
Also one trick when you want to open some java classes that you downloaded elsewhere is to create a new Java project. Then, move all your java classes into the src folder of the project. Now you can run with the option of "run as Java Application".
As a minor variation on the perfect answer of Reimus:
Add a String array argument to the main method as expected by the JVM
public static void main(String[] args) {
I had been struggling with my a script, when I ran it showed Run Configuration and Fail.
By changing the following:
public static void LaunchBrowser () throws InterruptedException {
to
public static void main(String[] args) throws InterruptedException {
it solved the problem.
My file name is Temp.java and inside it I have this. I'm using eclipse IDE
/*package*/ class Test {
public static void main(String args[]) {
System.out.println("test");
}
}
So I was unable to run this as java application. I change my class name to Temp
class Temp {
....
}
Now I can. Can someone explain me why ?
This is probably a limitation of Eclipse. The code runs well from command line.
As I understand, you are trying to embed your unit tests in the same file with the class under test. This is a nice idea and I totally concur with it. You can read more about how you can succeed in Ben J. Christensen's blog post. Generally, he suggests placing the tests in a static inner class, not a standalone class in the same file.
An example from the Netflix Hystrix framework: HystrixCircuitBreaker.UnitTest
The code below, located in Temp.java, compiles and runs fine with Netbeans:
class Whatever {
public static void main(String[] args) {
System.out.println("hello");
}
}
The problem is with eclipse, i think you are trying to run using right click -> run as -> Java Application, unfortunately eclipse is not showing this option if the class is not public.
But you can still run the class using Alt+Shift+X,J.
Its not the problem with Java, its with Eclipse.
The name of the file should be the same as the class name which is public and has the main() method. In your first case the file name Temp.java will compile and will create Test.class file not Temp.class because there is no Temp class declared in your file.
after .class file is created , run it with java Test
so here's an example
//Filename abc.java
public class hi
{
public static void main(String[] args)
{
System.out.println("Hell");
}
}
the output
abc.java:1: class hi is public, should be declared in a file named hi.java
public class hi
^
1 error
but if you do this
//Filename abc.java
class hi
{
public static void main(String[] args)
{
System.out.println("Hell");
}
}
it will create hi.class file so
D:\>java hi
Hell
The class (which main should be run) inside the .java file must have the same name as the file. If the class is not public (as in your case) the class will compile but it can't be run since Eclipse tries to load the class according to the file name.
I've been running different individual Java .java files in the Netbeans IDE by right-clicking the .java files themselves in the Project Explorer of Netbeans (the portion normally at the upper left part of Netbeans).
However, i've been googling on how to make a class file run another class file using code, but to no avail.
I have a project named "loadanotherfile" with 2 files, namely: Loadanotherfile.java and otherfile.java
I'm trying to make Loadanotherfile.java run otherfile.java, but I'm not exactly sure how. I read about Classloaders and URLClassloaders however these methods don't seem suitable for my purpose of running another .java file.
Below is the code of the 2 files i mentioned.
Loadanotherfile.java
package loadanotherfile;
public class Loadanotherfile {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
// TODO code application logic here
}
}
otherfile.java
package loadanotherfile;
public class otherfile {
public static void main(String args[])
{
System.out.println("This is the other file.");
}
}
I have a feeling that the task has something to do with using the "import" syntax (namely something like import loadanotherfile.* but even if my guess is correct, I'm still not sure on how to make my Loadanotherfile.java run otherfile.java using code.
How can I load otherfile.java using Loadanothefile.java?
Cheers
In Loadanotherfile.java
otherfile.main(args);
Compile the two together, and then from Loadanotherfile,
otherfile.main(args);
will do the trick. You don't need to import since you're in the same package. Note the linked tutorial.
I would investigate (however) class instantiation, and creating an instance of a new class to invoke upon. Invoking static methods from static methods isn't very OO.
Try This:
className.main(Args){
}
This works! ive tested it myself.
Check the public void main line. If there IOException and not there then insert
in Loadanotherfile.java
use this
otherfile.main(args);{
}