Run multiple java main classes at once in netbeans - java

I have several main classes with different arguments. I also added the arguments to each class successfully.
But the problem is: I have to start each class manually every time (e.g. click on Run-File).
Is there a solution where I can start all the classes with one click in netbeans? And the classes should also follow a specific order.

Open a new project with a specific name (File -> New project and complete the walk-thru) in NetBeans.
You can create any number of new classes under one project by going to File -> New File -> and completing the walk-thru. At this point of time you should not include main method in these classes. Do not open a new project each time.
Create yet another file (by going thru File -> New File etc.). This time in this new class include code for the main method. From the main method you can call any number of classes by creating instances of those classes. The classes will be executed in the order you called them under the main method as long as all those classes are included in the same folder, that is under the same project.
It looks like you are writing java programs just as do it in the procedural languages. To some extent java classes are like subroutines of the procedural languages. Calling them is done by creating an instance of that class.

Maybe call to each class seperately? For instance:
FirstClass.java
SecondClass.java
ThirdClass.java
In FirstClass, you could call on SecondClass to pop-up, simply with a setVisible(true) if that's all you want it to do. Then in the SecondClass call to ThirdClass to pop-up the same way.
I'm not sure if this is what you wanted as there's no code to go off of, but just something to get you thinking.

You can attempt to run multiple main classes via different run configurations.
See http://wiki.netbeans.org/FaqTwoMainClassesWithArguments

Set one class as main class through properties and run and in that you can Use following code:
ClassName variableName = new ClassName();
variableName.setVisible(true);
ex- Suppose my class name is Dog and I use frame as variable name
Dog frame = new Dog();
frame.setVisible(true);*emphasized text*

Drawing from comments and the question, I understand that you want to run ~5 different java programs simultaneously in your IDE (netbeans,) and the startup sequence has to be in a particular order. I assume you don't need CLI input for these programs once they're running.
I'm unaware of a netbeans way to accomplish your goal, although in Eclipse a Launch Group would satisfy your requirements.
IDE's aside, we can programmatically accomplish this goal anyway. The main() method in Java is just a static method, so if all your main methods are in one project, then you can just make a LaunchSequence class or something and do the following:
public static void main(String[] args)
{
Thread t1 = new Thread()
{
#Override
public void run()
{
ServerOneClass.main(whateverArgsItNeeds);
}
};
t1.start();
Thread t2 = new Thread()
{
#Override
public void run()
{
ClientOneClass.main(whateverArgsItNeeds);
}
};
t2.start();
//and so on.
//The order is enforced by the order you call start(), not the order in which you declare the threads
}
If you have the code for these things in different projects, then you could make a new project and add them as dependencies.
If you do actually need user input for all the programs, you may benefit from looking at Running a java program from another java program

Related

How and when to declare a Main Method

This may seem like a stupid queestion to most of you, but I have been learning Java using BlueJ, and BlueJ is created for learning, and as such Main Methods are not necessary given the BlueJ extensions provided by my University.
Therefore, now that I am playing around in both NetBeans and IntelliJ - I really want to get a good idea of when to declare a Main Method.
I know the Main method is the entry point of a package when it is compiled and run. But that is the extent of my knowledge.
If I am to build some apps in a full blown IDE, should I place all of my Class methods within a Main Method? Should the Main method be seperate from all others? Do I declare instance Variables within the Main Method?
Are there any good sites, or tutorial materials available that can help me structure my BlueJ Java knowledge into an IDE that requires a Main Method?
Thanks
Ok.
Main method is an entry point to your application.
If you watch any Java related tutorial, you'll probably see something like this.
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}
This has been taken from Oracle's website.
When working with IntelliJ, you'll be presented with a package main on the top, or something else.
You see. Java unlike some other languages distinguish between namespaces using those packages. Think of them as a little packages full of Java classes that you can reuse.
For example, if you look at Log4J's repo => here, all these folders are essentially packages.
That way you can have many packages, and a single file that will trigger your main method, and start everything else.
That's how it's usually done.
But if you really want to learn Java from scratch. My suggestion is go to YouTube. Derek has done some marvelous job with his tutorials.
Best of luck.
While technically you can have more than one main method, for most use cases you only need one.
You should define it where it makes sense to do so in some "central" class responsible for bootstrapping your application and getting everything going.
The main method is static though so it can't access your class' instance. Typically this is how you'd get around this.
class TicTacToe {
public void theEntryPointToYourApplication() {
// ...
}
public static void main(String[] args) {
// This doesn't necessarily have to be the same class where you
// defined your main method
TicTacToe ticTacToe = new TicTacToe();
ticTacToe.theEntryPointToYourApplication();
}
}
This is just a small example though there are many ways and different approaches depending on your specific use case and needs.
Personally I have never used BlueJ.I've always been a netbeans fan.But the answer to your question is a general rule that most of the Object Oriented Programming Languages follow.You know that main method is the entry point of a package when it is compiled and run.Let me explain it in simple language.
Consider a soccer game.Assume that the field is the main method.The players are different functions.You are free to define any number of functions, but only 22 functions are called in the field.The whole game is controlled and played by these 22 functions only.So what do you learn from this?
We learn that java is only concerned with what is present in its main method just like the fans sitting in the stadium are only concerned with the 22 players who are currently playing.So if a programmer is writing a small program,normally the whole logic goes into the main method.But if its a large program,the program is distributed into various modules/functions and then these functions are called in the main method so that they get executed.

In java, do I always need a Main class?

I know that I'll need a main method, but can that main method be in a different class other than the Main class?
Not all Java applications require a main method.
Java can also be used to create web applications, for instance, which don't require main methods to run.
The answer to your question depends on what exactly you mean. Do you mean a class with the name 'Main'? Then, no, there is no requirement for this at all.
The only requirement that Java has, is that the signature of the method is correct. the main method must:
be public
be main
be static
have returntype void
accept an array of Strings as (only) parameter
It's easier to add it in the public class in a file, but not mandatory. The name of the class it is in, is entirely up to you, though many will choose a name like 'Main' or 'Open', simply to more easily find it.
If you want to be able to run your application, by simple double-clicking the .jar file, you'll need to point to the class that contains the main method (to use: your application might contain a lot of main classes, used for internal testing, but only one can be used to start the actual application) in the manifest file: Manifest files
Prior to Java 7, it was possible to run a desktop application without a main method, by (ab)using an instantiation block, but this was removed as of Java 7, because this is not what the instantiation block was intended for.
It's not necessary to define yout main method in a main class. You can place your main method wherever you want, as long the syntax i correct :
public static void main (String[] args){
//...
}
You absolutely don't.
The method itself can be placed whereever you want it to be, there is no limitation.
However, I personally would recommend putting it in a class which at least contains something like "Main", because when others look at your code, and they are not using an IDE which supports jumping to the main method, people usually have an easier time finding your starting point.
However, that is just for sake of readability, and as I said, jumping to main is/should be usually a widespread supported feature
Yes, the Main method is required to run a function although a java class can be without the Main method. Though, it won't run...

why do we use java files without main ? What purposes does it meet ? How useful it is .?

I am new to java programming and i just came across to a java program composed of many files and only one of the file had a main function while others did not. I didn't really understood that why don't we have a main function in every java file.
Not every file needs a "main" function. For example, you may want to import a file with specific function or class. In this case (a java file without a "main" function), the java file simply represents a chunk of code, which is added to your program. So it doesn't need a separate "main" function, if you already have one.
In java, main() is an entry point to a program/application. Sometimes in a application we just need only one entry point to start the program and from this point other necessary code are used.
But not all application necessarily need an main() method - like web application or java applet where a container initiate the application/program.
Every Java program must have an entry point, and that entry point must have a certain signature. Which, as you have noticed, is public static void main(String[] args)
After that, the class that is the entry point can create instances of other classes and/or invoke their methods, or invoke static class-level methods. It is not necessary for those classes to have an entry point, because the program is already running (executing your code) in the Java Virtual Machine (JVM).
You'll notice that a common error novice (and sometimes even seasoned) programmers make is trying to run a program for which the runtime environment cannot find an entry point. E.g., Eclipse: Java, no main method found (in this example, the problem was that the OP had the wrong method signature). The runtime has to find something to, well, RUN.
If your code is going to be run by some other code (e.g., you are developing a library or an API that contains functionality that will be used as part of another application) it may not be necessary for you to have a main method. If you look through the .jar files of some of the standard libraries using your IDE, you should discover that very few of them have classes with a main method - this is because they are not intended to be run alone, but rather in the context of another application.
The public void main(String args[]) method in java is the entry point to the program. If it is run directly, it will start from the main method. However, not every class that you might make in Java is an acceptable entry point. In fact, in many applications, you should have exactly one main method. There is generally not too much need for more. Some java libraries has no main method anywhere at all. This code is generally designed so that it can be used by other java code, and often has no sensible way to run itself. It's not designed to do that. It is designed to help other programs that do.
What classes without a main method are used for, is a) to provide functionality for other java programs (dependancies for instance), b) to provide additional functionality to the program that is invoked through the main method.
Here is a couple example files that illustrate the second.
MainExample.java
public class MainExample{
public static void main(String args[]){
OtherClass other = new OtherClass()
other.doExpensiveComputation1();
other.doExpensiveComputation2();
}
}
OtherClass.java
public class OtherClass{
public void doExpensiveComputation1(){
//do stuff here
}
public void doExpensiveComputation2(){
//do other stuff here
}
}
Now, you might be asking, "couldn't I just write those methods inside the main class?". In some cases the answer might be yes, but for more complex code, it generally isn't. In some cases it will be impractical simply because it clutters up the main class too much to keep track of. It is much easier to keep track of code that keeps its classes in different files.
Execution of Java program always starts from Main method.Class having main method is the entry point for the program and further from there you can get the rest of the desired functionality.
If You want you can have main method in each java file, which will behave as multiple entry points for your program.

Eclipse export as runnable JAR

I know SO dislikes this type of questions but after googling and checking SO for close to an hour I am no closer to a solution. I have a package with some classes which together form a GUI based game which runs fine when I do CTRL-F11 but when I right-click the package->Export->Runnable JAR it is nowhere to be found in the Launch Configuration dropdown, while other packages/projects are.
I would like to be able to run this game outside of Eclipse even if I don't need to now. I have no main methods, my runnable uses acm. public class SokobanGFX extends GraphicsProgram.
Found this in Javadoc:
http://jtf.acm.org/javadoc/student/acm/program/Program.html
"In many programming environments, objects that are specific instances of a Program
subclass will run automatically without any special action on your part. For maximum
portability, you might want to define a static main method as described in the comment
for the standard implementation of main."
Should be easy to make an executable jar after that.
I'm not familiar with ACM, but based on my observations with the Javadoc, I believe:
public static void main(String[] args){
new SokobanGFX().start()
}
//http://jtf.acm.org/javadoc/student/acm/program/Program.html#main(String[])
Should launch the program.

Multiple main methods in java package

I have a project in NetBeans where two different classes have public static void main(String[] args) methods.
When I press F6, the first class' main is always invoked. Why not the second? When I'm trying to display arguments of args, it says that this array is empty.
In the project properties, in the Categories, choose Run.
then there is a Main Class.
Set your main class there
I don't know Netbeans, but in Eclipse it automatically sets up a launch config which you can easily customise to specify the full package and class name containing the main class.
Ultimately any program launch is going to effective be a command line execution so I'd expect that netBeans must have a way to customise it as well. You just need to find that and adjust the launch (or setup a new one) for your second program.

Categories

Resources