How to install javafx to Eclipse - eclipse

First of all, i am a newbie to Eclipse and Java.
So, my problem is, that i am unable to install javafx to my Eclipse.
What i have installed:
https://www.oracle.com/java/technologies/javase-jdk13-downloads.html (Windows x64 installer)
https://www.java.com/de/download/win10.jsp (download button)
What i already tried:
i followed this guide on how to install Maven and then javafx:
https://www.vogella.com/tutorials/EclipseMaven/article.html
For the POM.xml file i used the code from this link:
https://search.maven.org/remotecontent?filepath=org/openjfx/javafx/15-ea+1/javafx-15-ea+1.pom
Now, after creating a new Maven project and trying to import (e.g. javafx.scene.shape.Rectangle) i get this error:
(After importing jfxrt.jar)
Error: JavaFX runtime components are missing, and are required to run this application
An Example Code i want to run is from my friend:
package vierGewinnt;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Game extends Application{
public static void main(String[] args){
launch(args);
}
public void start(Stage primaryStage){
Field field = new Field();
Button newGame = new Button("Neues Spiel");
PositionColumn pos = new PositionColumn();
VBox root = new VBox();
root.getChildren().addAll(field, pos, newGame);
Scene scene = new Scene(root);
scene.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event) {
/*if(field.victory()){
Stage secondaryStage = new Stage();
HBox viBox = new HBox();
Scene viScene = new Scene(viBox);
secondaryStage.setScene(viScene);
secondaryStage.show();
}*/
if (event instanceof KeyEvent)
field.setStone(event);
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
}
(There are other classes for this to work, thats not the problem)
So, at this point i dont know what to next to make it work, or how to get javafx to work in a different way

You are using a JDK version 13 which no longer includes JavaFX. So you need to add JavaFX manually to your project.
This is quite easy, because you are using a Maven project. So all you need to do is:
open the pom.xml file of your project (in the top level directory of your project)
add the javafx dependency to your maven dependencies
(maybe) refresh your maven project (Alt + F5) for eclipse to notice the changes
To add the maven dependency to your project you simply add this to the pom.xml file (somewhere between the tags <dependencies> and </dependencies>:
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx</artifactId>
<version>11</version>
<type>pom</type>
</dependency>
Edit:
To answer your second question about the exception that occures when starting the application:
The StackTrace says:
Caused by: java.lang.IllegalAccessException: class
com.sun.javafx.application.LauncherImpl (in module javafx.graphics)
cannot access class vierGewinnt.Game (in module
org.openjfx.archetype_simple) because module
org.openjfx.archetype_simple does not export vierGewinnt to module
javafx.graphics
So the problem is that the class vierGewinnt.Game can't be created because it can't be accessed. That's because it is not exported. If you add the export it should work.

Related

How to update JUnit5 jupiter.api_5.3.1 to jupiter.api_5.4.2?

i want to know that if "MethodOrderer" class is available in JUnit5 Library FOR ECLIPSE or not, because i am unable to find it.
If not, how can i shift jupiter.api_5.3.1 to jupiter.api_5.4.2 in eclipse JUnit5 library?
Will be thankful to see your reply.
I downloaded JUnit5 jar file from "https://search.maven.org/artifact/name.remal.tools.test/junit5/1.26.97/jar" and this jar does have "MethodOrderer" class but when i add this to project dependency and run the testclass, eclipse shows up this error "No tests found with test runner 'JUnit5'."
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
#TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class JunitCalculatorV4 {
#BeforeAll
static void setUpBeforeClass() throws Exception {
System.out.println("Before All");
}
#AfterAll
static void tearDownAfterClass() throws Exception {
System.out.println("After All");
}
#Test
#Order(1)
void addTest() {
System.out.println("Add test");
}
#Test
#Order(2)
void divideTest() {
System.out.println("Divide Test");
}
}
Actually this annotation #TestMethodOrder(MethodOrderer.OrderAnnotation.class) is from jupiter.api_5.4.2 which i added as an external jar, and that might be causing conflict with the existing JUnit5 library.
My problem would be solved if the JUnit5 library is updated as a whole, or atleast the jarfile inside the library is updated.
Project > Properties: Java Build Path, tab Libraries:
You are using Eclipse 2018-12 (4.10) with JUnit 5.3.1 instead of Eclipse 2019-03 (4.11) with JUnit 5.4.0 (the screenshot shows JAR file names containing _5.3.1.v20181005- instead of _5.4.0.v20190212-).
Please upgrade.

Not able to create executable jar for webdriver+TestNg project which does not contain main method in any class

I am learning webdriver and I have created one project with TestNg.
I have several classes in my package under src folder.
No class contains public static void main(....). i.e[Entry Point]
My question is :
Can we create Runnable / Executable jar file through eclipse for projects like this[project without main method]. I searched on many sites but unfortunately didnt get solution.
Please suggest me some links OR The way by which we can do this.
To create a jar file of the TestNG without main method you have to create another class which contain main method.
Suppose you have a TestNG file name as Sample.java, in the same package create one more class as ExecutableRar and write the below code :
public class ExecutableRar {
public static void main(String[] args) {
TestNG testng = new TestNG();
Class[] classes = new Class[]{Sample.class};
testng.setTestClasses(classes);
testng.run();
}
Now you can run this class as a Java Application. Then right click on the package --> Export --> Java --> Runnable jar File --> select ExecutableRar in launch configuration --> Browse the file location and enter the name of the file in Export Destination --> Finish.
Please let me know if you are having any issues.

Importing greenDAO from eclipse to Android studio 1.0 RC 2 [duplicate]

I'm looking for a clear step-by-step explanation on how to import GreenDao in Android Studio.
I've used it before in AS, but failed to get it to work again.
There are some tutorials out there, but they don't seem to apply to the latest version of AS.
When I clone from github, I get a example project stuff etc.
Is there a way to install GreenDaoGenerator without these extras?
Just looking for an up-to-date step-by-step explanation.
Update: I suggest using Realm.io now! Check it out! :-)
Any help would be appreciated!
Tested on Android Studio 2.0
With Android Studio 0.6.1+ (and possibly earlier) you can easily add non android project to your android project as a module.
Using below method you can have Java modules(greenDaoGenerator) and Android modules in the same project and also have the ability to compile and run Java modules as stand alone Java projects.
Open your Android project in Android Studio. If you do not have one,
create one.
Click File > New Module. Select Java Library and click Next.
Fill in the package name, etc and click Finish. You should now see a
Java module inside your Android project.
Open the build.gradle file of the java project and add the following dependency
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile('de.greenrobot:DaoGenerator:1.3.0')
}
Copy your DaoGenerator classes or create if you don't have one to your java module.For e.g. I have created ExampleDaoGenerator class in my java module.
public class ExampleDaoGenerator {
public static void main(String[] args) throws Exception {
Schema schema = new Schema(1000, "de.greenrobot.daoexample");
addNote(schema);
new DaoGenerator().generateAll(schema, "../DaoExample/src-gen");
}
private static void addNote(Schema schema) {
Entity note = schema.addEntity("Note");
note.addIdProperty();
note.addStringProperty("text").notNull();
note.addStringProperty("comment");
note.addDateProperty("date");
}
}
Now, to generate the classes that you can use in android project follow below steps.
Click on the run menu in the top bar. Click Edit Configurations...
In the new window, click on the plus sign at the top left of the window and select Application
A new application configuration should appear, fill the following information.
Give it a name e.g. greenDao.
In main class click … button and select your generator class which have the main method.for e.g. in this case it is
com.greendao.generator.ExampleDaoGenerator
In working directory select path of your java project.
In use class of module select you java project.
click ok.
Again go to run menu and now you can see e.g. run greendao. click on it.It should compile successfully.
Its done !!! you can check your generated classes in the folder that you have specified.For e.g. in this case it is /DaoExample/src-gen
NOTE: You can run your android project again by clicking on run menu -> Edit Configuration . select your project and click ok.
Here's a step by step overview for Integrating GreenDao into your Android Project.
[ Reference How to use GeenDao with Android ? ]
[Project Link: GreenDao Example ]
PART1 : Setting Up GREENDAO
Create an android project.
Click File >New > New Module. Select Java Library and click Next.
Now we have to add the following Gradle Dependencies.
In build.gradle of Module:app, insert
compile 'de.greenrobot:greendao:2.1.0'
In the build.gradle of Module:greendao-generator, insert
compile 'de.greenrobot:greendao-generator:2.1.0'
Make sure, you sync your project.
Now in the MainGenerator.java,
we will define the database structure.
import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Schema;
public class MainGenerator {
public static void main(String[] args) throws Exception {
//place where db folder will be created inside the project folder
Schema schema = new Schema(1,"com.codekrypt.greendao.db");
//Entity i.e. Class to be stored in the database // ie table LOG
Entity word_entity= schema.addEntity("LOG");
word_entity.addIdProperty(); //It is the primary key for uniquely identifying a row
word_entity.addStringProperty("text").notNull(); //Not null is SQL constrain
// ./app/src/main/java/ ---- com/codekrypt/greendao/db is the full path
new DaoGenerator().generateAll(schema, "./app/src/main/java");
}
}
Run MainGenerator.java
After running this, you will observe a newly created folder i.e. db in the Main Project Folder.
PART2 : Integrating it with Android Project
Set the activity_main.xml layout.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textData"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
android:id="#+id/textSave"
android:layout_below="#+id/textData"
android:layout_alignEnd="#+id/textData"
android:layout_marginTop="22dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Top"
android:id="#+id/textTop"
android:layout_below="#+id/textSave"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp" />
In MainActivity.java,
Add the following codes
package com.codekrypt.greendao;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.codekrypt.greendao.db.DaoMaster;
import com.codekrypt.greendao.db.DaoSession;
import com.codekrypt.greendao.db.LOG;
import com.codekrypt.greendao.db.LOGDao;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//Dao --> Data Access Object
private LOGDao log_dao; // Sql access object
private LOG temp_log_object; // Used for creating a LOG Object
String log_text=""; //Entered text data is save in this variable
private final String DB_NAME ="logs-db" ; //Name of Db file in the Device
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialise DAO
log_dao=setupDb();
//Setting up form elements
Button textSave= (Button) findViewById(R.id.textSave);
Button textTop= (Button) findViewById(R.id.textTop);
final TextView textData=(TextView) findViewById(R.id.textData);
assert textSave != null;
textSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
log_text=textData.getText().toString();
temp_log_object=new LOG(null,log_text);// Class Object, Id is auto increment
SaveToSQL(temp_log_object);
}
});
assert textTop != null;
textTop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textData.setText( getFromSQL() );
}
});
}
//---------------------------------SQL QUERY Functions-----------------------------------------//
public String getFromSQL(){
List<LOG> log_list = log_dao.queryBuilder().orderDesc(LOGDao.Properties.Id).build().list();
//Get the list of all LOGS in Database in descending order
if(log_list.size()>0) { //if list is not null
return log_list.get(0).getText();
//get(0)--> 1st object
// getText() is the function in LOG class
}
return "";
}
public void SaveToSQL(LOG log_object) {
log_dao.insert(log_object);
}
//----------------------------***END SQL QUERY***---------------------------------------------//
//-------------------------------DB Setup Functions---------------------------------------------//
//Return the Configured LogDao Object
public LOGDao setupDb(){
DaoMaster.DevOpenHelper masterHelper = new DaoMaster.DevOpenHelper(this, DB_NAME, null); //create database db file if not exist
SQLiteDatabase db = masterHelper.getWritableDatabase(); //get the created database db file
DaoMaster master = new DaoMaster(db);//create masterDao
DaoSession masterSession=master.newSession(); //Creates Session session
return masterSession.getLOGDao();
}
//-------------------------***END DB setup Functions***---------------------------------------//
}
Before Running the App, Make sure you have changed your configuration.
Now Run it.
PART 3 – VIEW THE SQL DB
Open Command Prompt.
Enter the following commands.
Opening the db file in SQLite3
Using SQLite3
PART 4 – EXTRAS
Structure (Core Classes) of GREENDAO
I have used this tutorial for Android Studio 0.8.9 and everything works fine.
Works on Android 1.3 Preview
For the top answer ( Tested on Android Studio 1.0 ), you might need to include that source folder in your project. Go to app/build.gradle
add the following inside android block
sourceSets{
main{
java{
srcDir 'src-gen'
}
}
Solution: IO-Exception
Go to the build from your dao generator.
add: apply 'application'
add: mainClassName = "you.package.include.Main"
execute "run" in application task (gradle task)
I dont know why it doesnt work when you create manually a run configuration.
Basically, what you need is to add a Java library module (File > New > New module..) to your Android project (assuming you're using Android Studio), and insert the generation code inside public static void main(String[] args) {} in this module's .java class. Then Run it and the code will be generated in you main app's module.
See this blog post for a step by step tutorial with explanation.

JAVA EE & Eclipse: I am getting java.lang.NoClassDefFoundError even though I have set the build path

I am using Eclipse with the Google App Engine plugin. I'm trying to run a simple program with added joda time. It seems like the error relates to the build path and I followed the instructions in:
https://stackoverflow.com/a/12105417/3255963
but I am still getting the error below. What do I need to do to next?
package test;
import java.io.IOException;
import javax.servlet.http.*;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
#SuppressWarnings("serial")
public class testServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
DateTime newYears = new DateTime (2014, 1, 1, 0, 0);
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
}
}
Error:
java.lang.NoClassDefFoundError: org/joda/time/DateTime
I see the joda-time-2.3.jar in the project explorer and the build path.
I also tried selecting it under order and export.
NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time.
Please ck whether u have the req. jars under \WebContent\WEB-INF\lib in the project explorar as well as on the project build path.

OSGI Bundle Installed and Started but no visible output

I am trying to learn OSGI. (Mainly, the dynamic loading and unloading of bundles).
Following Neil Bartlett's tutorial for How To Embed OSGi, I added The Equinox OSGi framework implementation to the class path and started the game.
Here's the code:
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;
public class BundleManager {
/**
* #param args
* #throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FrameworkFactory frameworkFactory = ServiceLoader.load(
FrameworkFactory.class).iterator().next();
Map<String, String> config = new HashMap<String, String>();
//TODO: add some config properties
Framework framework = frameworkFactory.newFramework(config);
framework.start();
BundleContext context = framework.getBundleContext();
List<Bundle> installedBundles = new LinkedList<Bundle>();
installedBundles.add(context.installBundle(
"file:C:/Users/student/Documents/eclipse/myPlugins/HellowService.jar"));
for (Bundle bundle : installedBundles) {
if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) == null)
bundle.start();
}
System.out.println("done!!");
}
}
Yes, it works. No errors at all. However, the bundle that I installed which is a jar file in the path:C:/Users/student/Documents/eclipse/myPlugins/HellowService.jar contains a "HelloWorld" in its start method. I don't see that "HelloWold" in my eclipse console. Why I don't see that message although the bundle was started? I appreciate any simple help.
Note: HellowService.jar is a plugin project that i created earlier, implemented BundleActivator in one of its classes to add "HelloWorld" message in the start method, and finally exported it as a jar file to the directory C:/Users/student/Documents/eclipse/myPlugins/
Edit: Here's my Activator class in the bundle I am installing and starting:
package com.javaworld.sample.service.impl;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import com.javaworld.sample.service.HelloService;
public class HelloServiceActivator implements BundleActivator {
ServiceRegistration helloServiceRegistration;
public void start(BundleContext context) throws Exception {
HelloServiceFactory helloServiceFactory = new HelloServiceFactory();
helloServiceRegistration =context.registerService(HelloService.class.getName(), helloServiceFactory, null);
System.out.println("Hello World!");
}
public void stop(BundleContext context) throws Exception {
helloServiceRegistration.unregister();
}
}
And here's the MANIFEST.MF file of the bundle:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloService
Bundle-SymbolicName: com.javaworld.sample.HelloService
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.javaworld.sample.service.impl.HelloServiceActivator
Bundle-Vendor: JAVAWORLD
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: org.osgi.framework;version="1.3.0"
Export-Package: com.javaworld.sample.service
The way i export the bundle is by Right Click on the bundle project->Export->Runnable Jar File->then I select the Launch Configuration to be BundleManager (Which is the class installing the bundle).
I still do not see "Hello World!" message when I start the bundle from my application.
Your launcher does not wait for the OSGi Framework to stop. I would expect this program to start everything but then immediately shut down, because we reach the end of the main method. Refer back to my tutorial where I show how to use the Framework.waitForStop method.
Having said that, I would expect the output from your HelloWorld bundle to actually appear before the shutdown. So it seems likely there is an error in that bundle also. Perhaps you haven't declared the activator? I can only guess, because you haven't given any details.
It turned out that I was exporting the bundle incorrectly. That's because I tried to do it by myself. Here's how the bundle should be exported as a jar file:
Open the plugin export wizard File > Export... > Plug-in Development >
Deployable plug-ins and fragments .
Then select the bundle you want to export and the destination directory. Done!
You can now use the path of the jar file to install the bundle. In my case, it is:
installedBundles.add(context.installBundle(
"file:C:/Users/student/Documents/eclipse/myPlugins/plugins/com.javaworld.sample.HelloService_1.0.0.201307220322.jar"));
Source: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.pde.doc.user%2Fguide%2Ftools%2Fexport_wizards%2Fexport_plugins.htm
And for sure thanks to #Neil Bartlett