Eclipse export as runnable JAR - java

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.

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.

Mate libGDX and Jetbrains MPS

I also posted this question on the LibGDX forums.
Hey there! For my thesis, I'm writing a DSL to describe the look of pictures. These pictures will be painted by libGDX according to the DSL-Input.
By now, jetbrains MPS (v. 3.0) and the newest libGDX-version is in use. My actual problem is: how to design the interconnection between my MPS-language and the java-libGDX-picture-generator.
Jetbrains MPS is not widely spread, but I'm sure, my problem can be solved without knowledge of it.
In the DSL, a generator is designed to build a class that can be executed (public static void main). Here goes several method calls to start picture-generation. Think of it as generatePicture(200px, 150px, "blue") (it's much more complicated, but I think that's not important for my problem).
In libGDX I have several launchers (especially the DesktopLauncher), these are the programms main classes, the ones that were started. At the moment, the launcher starts another class (I named it "Main") and here are the method calls.
The libGDX programm is inserted into MPS via jar artifact, so it's methods can be used in my DSL.
What would be a good solution to make my DSL-code start my libGDX-programm?
I have to make all these method calls and start the launcher. I thought about an additional class to initialize the launcher and make the calls, or trying to insert the calls from the DSL into the jar-classes.
Are there any comparable problems out there or someone who faced this very issue? I'm sure I will bring it to work somehow, but I'm interested in a nice and smooth solution.
Edit
The problem is that I'm somewhot sure I need the DesktopLauncher and its config to run the libGDX stuff inside my PictureGenerator.
Edit 2 - first approach
This is MPS related again. I thought about generating the Picture class with the MPS generator, but run the Launcher class.
Is this somehow possible? To run a class from the solution it need to implement IMainClass, but the Launcher couldn't. The launcher will always look the same. Is there a functionality to run another class than the generated one?
Or on libGDX-site: is it possible to merge launcher and Picture-class to insert the config?
Look at robot_Kaja sample (in MPSSamples.3.3\robot_Kaja). The Script concept implements IMainClass interface which makes it possible to run instances of this concept. You can right-click on any script in the jetbrains.mps.samples.Kaja.sandbox solution and you will see a Run option in the popup menu. Clicking on it will run the generated code for this script.
In your case, you probably also have some top-level concept similar to Script, which is generated into the Java (baseLanguage) Main class. Just make this concept implement IMainClass and it should become possible to run it directly from MPS.
Ok, I made it, following the idea of my second edit.
one file is generated in MPS
the generated file can be executed
a libgdx jar file is used
In MPS, I generate the following class with an inner class
public class DesktopLauncher {
public static void main(string[] args) {
LwjglApplicationConfiguration config = new LwgjlApplicationConfiguration();
new LwjglApplication(new Picture(), config);
}
public static class Picture extends ApplicationAdapter {
public Picture(){}
PictureGeneratorImpl generator;
public void create() {
generator = new PictureGeneratorImpl();
}
public void render() {
generator.generatePicture();
generator.exit();
}
public void dispose() {
generator.dispose();
}
}
}
As additional information: my libgdx jar contained the whole project (including core and desktop).
I hope this might help anyone who got the idea to combine MPS and libGDX.

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.

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.

C++ .NET equivalent to java public static void main()?

In java I can simply test classes directly with
public static void main()
I then just add quick code and under Eclipse "Run" the class. Is there anything similar in C++ .NET ?
Right now I have to create an empty project, reference the correct headers then set that project as start up project.
Unit tests. There's also the object test bench if you're using VS.
I do this in C# so I don't know if this will react any differently, but I set up an empty "test" class with the main method and then set the project to startup with that class file. You shouldn't have to create the file in a separate project.
I'm not terribly familiar with Eclipse but if you're just looking to run your objects in the IDE do the following.
Open up the Immediate window and just call whatever function you want. This will start execute the code you type. You will likely have to qualify the name. Ex: ClassLibrary1.MyClass.SomeMethod()
I like using TestDriven.NET for this. It allows you to execute any public method by rightclicking on the header and selecting "Run test".
I like making
public static void Test()
methods on dialog- and form-classes to use with this feature.
It supports C++/CLI, so if that is what you mean by C++.NET, it should work for you.
Edit: You should only do that for things that are not automatically testable - such as pure GUI classes. Otherwise I agree with the other commenters: use a unit test framework.

Categories

Resources