Java Programming using eclipse - java

how do you compile in Eclipse? And how do you run the program using this given with the choices: Java Applet or Java Application?

If Project -> Build Automatically is enabled, you will compile when saving your work.
To run it, you have an arrow in a green circle in your toolbar (6th tool if you have a default benchmark). It will run the project using the currently showing class as a main class if it has a main method, or the last run main if it doesn't. Alternatively, you can use the keyboard shortcut CTRL+F11.
If you click on the little down arrow next to the green button, you can access to a history of classes you ran and to the Run configuration, which allows you for example to run a program with parameters or, what interrests you here, to choose to run it as an applet or an application.

Turn on the option: Project > Build Automatically
If you have a main method, run as Java Application. To create a main, type main then ctrl+space and select "main method" to have eclipse auto create it for you.

Related

Run A Simple Java Program in Eclipse?

I have an assignment for school where I need to download files from github, edit code, and run and test them. I downloaded the files into a general project folder in eclipse, but can't seem to figure out how to run the java files within eclipse. So far I've been getting by with just using the command line, but it would be much faster and more efficient to do everything within eclipse.
There are multiple java files each with a main class. They are all under the same project and I don't want to have to make individual projects for each. Most of my googling has had the answer "right click -> run as -> java project", but I am using eclipse neon and do not see that option. When I try to set up a run configuration, the project does not show up for the project field and if I manually enter it it says that the project does not exist. Any ideas on how to get this working? Thank you.
Edit- The way that I got the files from github was by clicking "File -> Import -> Projects from Git -> Clone URL -> Master (Next) -> Next ->
The simplest way to launch a Java program is to run it using a Java Application launch configuration. This launch configuration type uses information derived from the workbench preferences and your program's Java project to launch the program.
In the Package Explorer, select the Java compilation unit or class file with the main method you want to launch.
Press the Run [ The Run button ] button in the workbench toolbar or select Run > Run from the workbench menu bar. Alternatively, select Run As > Java Application in the Package Explorer pop-up menu, or select Run > Run As > Java Application in the workbench menu bar, or select Run As > Java Application in the drop-down menu on the Run tool bar button.
Your program is now launched, and text output is shown in the Console View.
I downloaded the files into a general project folder in eclipse
This is your problem - to launch java classes you want a java project
OK. It appears that I may have figured this out and have things the way that I want them. Thanks to #markspace for this link that describes a similar problem. The simple solution was to create a new java project that had the same name as the github folder that I was downloading from, then change the source from default to the local git folder where the files are located. I could then use the other recommended solutions to right click, then run as java application to run everything within eclipse. Thank you everyone for your help.

How can we test plain Java code in Android Studio?

If I have a project in Eclipse, I can at any time create a class with a main and test anything having all the classes and libraries of my project available.
How do I do something like that in Android Studio?
If I have a big project and I would like to test a small method in isolation, how would I do that without needing to run the emulator or an Activity, etc?
To create a run/debug configuration for a class or method in your Java code, follow these steps:
Open a project in Android or Project view.
Open a Java file in the Code Editor.
Select a class or method in the code, and then press Ctrl+Shift+T (⌘⇧T).
Select Create New Test from the menu that appears.
In the Create Test dialog, optionally change the field values, and then click OK.
In the Choose Destination Directory dialog, select androidTest to create an instrumented test or test to create a local unit test. Click OK.
The new test appears in the Project window in the corresponding test source set.
To run the test, do one of the following:
In the Project window, right-click the test and select Run or Debug.
In the Code Editor, right-click a class definition in the test file and select Run or Debug to test all methods in the class.
In the Code Editor, right-click a method name in the test file and select Run or Debug to test just that method.
In the Code Editor, click Play in the left margin next to a class or method and select Run or Debug.
To save the configuration, select Save from the Select Run/Debug Configuration drop-down list within the toolbar.
The drop-down list is to the left of Run and Debug ; for example, .
Alternatively, right-click the item again and select Save.
Or, select the configuration in the Run/Debug Configurations dialog and click Save Configuration on the upper lef
SOURCE
Just right click on your class and select Run YourClassName.main()
If The Class you are using only use plain old Java no android.* You can make a Run Configuration just like you would do in Eclipse. Otherwise You need to Write JUnit tests and run them on the device.
Personally, I have a separate small, pure Java module that's mostly for that purpose and is included more or less as a library in my app. I also toss JUnit there for those classes I need to test, and Gradle executes those when the app is built.
Not sure if that applies to your scenario, as I am not sure what types of things you are testing - Java methods or Android libraries, but from what you describe, what is preventing you from running a Java class with a main method on its own? You'd right-click a class in Eclipse and select the Run option there. That also exists in Android Studio.
You can run your class file by clicking CTRL+SHIFT+F10

Running a program in eclipse

Each time I try to run a new program, an old program 'client.java' is run by eclipse. How do I change this setting? How do we tell eclipse which program to run? I tried using the arrow beside run button but it dosen't list out my new program.
You can change the default behavior by going to Window > Preferences > Run/Debug > Launching and in the 'Launch Operation' section, select the radio button for Launch the selected resource or active editor and then select Launch the associated project underneath.
Right Click To the class -> Run As-> JAVA Application
Firs of all make sure that your program has a main class.
Then click in the black button next to the run button -> Run As-> JAVA Application.
If this doesn work make sure you have all this properly set up.
Run Configurations

Can you force eclipse to run/debug another project when a particular one is ran/debugged?

I'm working with Ligdx, an API for cross plattaform game development that by default generates at least 2 projects:
The core project: This is the one you are supposed to work on, where all the classes and logic should be placed.
The desktop project: This one is automatically generated from the core project.
The core doesn't have any class to start it because it's supposed to be started from the desktop (or other platforms) project.
It is also always selected -because its what you are working on- so pressing the "run" or "debug" buttons will attempt to run the core project and not the desktop one and immediately fail thus forcing you to select the desktop project before hitting run/debug. This can get extremely annoying, especially when debugging.
So is there a way to force Eclipse to always run the desktop project?
Rekaszeru's answer to this question shows how to set up Eclipse in order to always launch the previously launched application.
You simply have to go to preferences / run-debug / launching and check the "Always launch previously launched application" box as he shown in the picture he used to illustrate his answer:
This will not force one of the projects to always be launched but it would pretty much solve the problem unless you are working on multiple projects at the same time or you alternate between Web/Android/Desktop.
If Your asking how to start a Core project from already existing project present in the eclipse then all you need to do is Right Click on desktop folder of the project and go over Run As and select Java Application
But if that is not what you have posted for I can only understand that you need to understand how a LibGDX project is deployed.... From Desktop project "In xyzMain.java" file you will have a method call to the core project saying:
new LwjglApplication(new Sample(), cfg);
This is where you'r Sample Class from the core project is called over.
But from the title I get a diff feeling that you wish to add another project to your already existing project...
You can do that by changing the build Path of the project....by including Other proj to the one you wish to compile and run.
Hope this helped you out..
I´m not aware of doing exactly what you have described, but I´d suggest using the Launch Configurations menus and dialgos to do what you want in a similar quick way.
Note that you can configure your launch configurations doing: Run -> Run Configurations... (also Run -> Debug configurations...)
Option 1).
Once you have run your Desktop Project, simply use the Run Drop-down button, which contain your launch configurations:
Option 2).
Save a launch configuration file in your Core project (or wherever you want) using the last "common" tab of the launch configuration dialog:
Then you can simply right-click on your that file to launch your Desktop Project:
P.S: I´d also suggest reading a little bit about run configurations in lars tutorial

Stanford CS106a: some java applet errors that do not allow to run the programs

Wasnt able to solve the problems in this screen.
http://s8.postimage.org/n5osf76ed/qestions.jpg
All the code worked a month ago.
What have I changed: I have a different version of eclipse (now: Eclipse IDE for Java Developers 1.5.1.20120920-0737, don't know what the previous one was).
Messed with environment variable. Since this does not effect the ot
Please help.
Try this solution mentioned in this discussion.
I just ran into this problem using standard (not Stanford) Eclipse.
By now this is probably moot for you, but I'll post my solution for
future travelers.
The launch configurations for Assignment 1 default to Applet, so the
containing Karel application isn't launched. To solve this, create
and use new Application launch configurations for each file.
If you already tried to run some of the files, delete the Applet
configs created so you can reuse the names:
Open the project properties (e.g. right click on Assignment 1 and select Properties).
Select Run/Debug Configurations.
Select and Delete each configuration.
Click OK.
May the Force be with you.
Create Application configs:
Right click on java file (e.g. CollectNewspaperKarel.java) and select Run As > Run Configurations...
Select Java Application in the left pane.
Click New Launch Configuration (the icon of a document with a +).
Type a name for the configuration and set its Main class to the associated file (e.g. CollectNewspaperKarel).
Click Apply then Run. (This should run correctly and create a useful entry in the Run History.)
Eclipse will still try to default to Applet, so you will henceforth
need to launch these configurations from the Run History (either from
the Run menu or the pull down on the run button). If you accidentally
launch as default, it will add a useless Applet configuration to the
Run History (e.g. CollectNewspaperKarel (1) ). You can delete this
from the Run Configurations dialog to keep the Run History tidy.
The eclipse used in CS106A is customized for stanford and for "karel the robot" :)
Just download and install it again and things should work just fine.

Categories

Resources