I want to open pdf files using the code below, but method getHostServices(); is not found even though i have the right imports.
import javafx.application.HostServices;
ClinicFiles clinicFiles = (ClinicFiles) listViewClinic.getSelectionModel().getSelectedItem();
HostServices hostServices = getHostServices();
hostServices.showDocument(clinicFiles.getAbsolutePath());
Probably, your class is not extending from javafx.application.Application:
For example:
import javafx.application.Application;
import javafx.application.HostServices;
public class App extends Application {
public void method(String[] args) {
ClinicFiles clinicFiles = (ClinicFiles) listViewClinic.getSelectionModel().getSelectedItem();
HostServices hostServices = getHostServices();
hostServices.showDocument(clinicFiles.getAbsolutePath());
}
}
Related
I would like to use Koin library from Java. According to this tutorial, Koin modules can be written in Kotlin even using Java in the rest of a project.
https://insert-koin.io/docs/quickstart/android-java/
I use also #JvmField annotation for the module field because in the source code of this tutorial it is used
https://github.com/InsertKoinIO/koin/blob/master/quickstart/getting-started-koin-android/app/src/main/kotlin/org/koin/sample/AppModule.kt
In my project I use koinInjector which is a list of modules
import org.koin.android.java.KoinAndroidApplication;
import org.koin.core.KoinApplication;
import static org.koin.core.context.DefaultContextExtKt.startKoin;
import static com.x.y.KoinInjectorKt.koinInjector;
public class App extends Singleton {
#Override
public void onCreate() {
super.onCreate();
KoinApplication koin = KoinAndroidApplication
.create(this)
.modules(koinInjector);
startKoin(koin);
}
}
KoinInjector.kt:
#JvmField
val koinInjector: List<Module> = listOf(
localDbModule,
)
But during build I get the error:
error: cannot find symbol
import static com.x.y.KoinInjectorKt.koinInjector;
^
symbol: class KoinInjectorKt
location: package com.x.y
I had to add
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
to build.gradle and
apply plugin: 'kotlin-android' to build.gradle (app)`
Now it works.
Thanks #Jeroen Steenbeeke for your comment
Try to replace the code with -
import org.koin.android.java.KoinAndroidApplication;
import org.koin.core.KoinApplication;
import static org.koin.core.context.DefaultContextExtKt.startKoin;
import static com.x.y.KoinInjectorKt.koinInjector;
public class App extends Singleton {
#Override
public void onCreate() {
super.onCreate();
KoinApplication koin = KoinAndroidApplication
.create(this)
.modules(listOf(localDbModule));
startKoin(koin);
}
}
remove this import
'import static com.x.y.KoinInjectorKt.koinInjector;'
and write your code like this
public class App extends Singleton {
#Override
public void onCreate() {
super.onCreate();
List<Module> list = new ArrayList<Module>();
list.add(localDbModule);
KoinApplication koin = KoinAndroidApplication
.create(this)
.modules(list);
startKoin(koin);
}
}
I'm sure, this question can be answered very quickly by a experienced Java-developer. But as I am not that familiar with Java I don't get how to source out the #Config part of Selenium in Java. It would be optimal, if I could have a config-file or -class where I can put the data (browser, website etc.) on the one hand and on the other hand the test-files.
Here is an example of a test file:
package com.example_test.selenium;
import io.ddavison.conductor.Browser;
import io.ddavison.conductor.Config;
import io.ddavison.conductor.Locomotive;
import org.junit.Test;
#Config(
browser = Browser.CHROME,
url = "http://example.com"
)
public class test_a_Home extends Locomotive {
#Test
public void testifExists() {
validatePresent(site_a_Home.EL_NEWCUSTOMERBANNER);
}
}
Now I would like to have a seperate file called tests.java where I can call the "test_a_Home"-function. If I try it just with
package com.example_test.selenium;
public class tests {
test_a_Home test = new test_a_Home();
test.testifExists();
}
I am receiving the error, that "testifExists()" cannot be resolved.
I tried changing the public void testifExists() to public int testifExists() and tried to call it with int res = test.testifExists(); in the class tests but this does not work either, as I receive the error java.lang.Exception: Method testNewCustomersBannerExists() should be void.
I would be very happy, if anyone could help me. If you need more information please feel free to mention it. Thank you.
If you want your design to be like this, then you need to organize your tests as such:
public class BasePage {
public Locomotive test;
public BasePage(Locomotive baseTest) {
test = baseTest;
}
}
public class test_a_Home extends BasePage {
public test_a_Home(Locomotive baseTest) {
super(baseTest);
}
public void testifExists() {
test.validatePresent(site_a_Home.EL_NEWCUSTOMERBANNER);
}
}
Then your test class, i'd recommend creating a base class as well:
#Config(
browser = Browser.CHROME,
url = "http://example.com"
)
public class BaseTest extends Locomotive {}
And then your test class:
public class tests extends BaseTest {
test_a_Home test = new test_a_Home(this);
#Test
public void testHomePage() {
test.testIfExists();
}
}
Also you state state:
I don't get how to source out the #Config part of Selenium in Java.
Please make sure you know, that using Conductor abstracts you from the Selenium API.. It just wraps it. #Config does not belong to Selenium, it belongs to Conductor.
i am creating a little game with libgdx framework and netbeans 8. I have all java classes in a single package that match with the directory structure.
The problem is that i cant import or isntantiate classes, for example:
package com.myfolder.folder2;
import ...
public class myclass1{
private myclass2 mc2;
etc...
}
In this case myclass2 is public and is inside the package but netbeans complains "cannot find symbol".
If i try with alt+enter, netbeans says "Create class myclass2 in package com.myfolder.folder2" or the same like an inner class. If i press the first option, netbeans create a class in the package with the file name myclass2_1 (becouse myclass2 exists!), and myclass1 doesnt recognize the new class.
If i try to import the class:
import com.myfolder.folder2.myclass2;
It gives me the same error, and in fact the code completion tool only gives me one crazy option in the import sentence:
import com.myfolder.folder2.myclass1;
Import the same class.
What can i do? I never have these problems using netbeans.
PD: Sorry for my english :)
You can use a class inside the same package like this:
ClassName classVariableName = new ClassName();
Then when you want to run something from the class you would put
classVariableName.MethodThatIWantToRun();
Or if you want to access a property from that method you would access it in a very similar way:
classVarabileName.PropertyIWantToAccess
Example:
You have one class with a property you want to access:
class MyClass {
public int MyProperty = 5;
}
You access it in this class:
public class MainClass {
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass.MyProperty);
}
}
If that doesn't work than you might have some other problem.
It was an error with one of my class package definition:
public class DesktopLauncher{
public static void main(String... args){
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
.
.
.
new LwjglApplication(new MyClass, config);
}
}
It was in MyClass, becouse i copied a snippet from an older project, and accidentally copied the older package.
NetBeans is not smart enough,
Solution: just declare the package name in all classes, example:
Class A:
package test;
public class ClassA {
public static void main(String[ ] args) {
ClassB.myFunctionB();
}
}
Class B:
package test;
public class ClassB {
public static void myFunctionB () {
System.out.print("I am ClassB!");
}
}
I am trying to create a custom Emitter for BIRT. I created a Plugin, here works everything fine the problem is that I need it as a Java class.
The problem is when I try to Render in my own Emitter it says that the custom RenderOption is not supported.
Here is some of my Code:
IJsonRenderOption:
package org.eclipse.birt.report.engine.emitter.json;
import org.eclipse.birt.report.engine.api.IRenderOption;
public interface IJsonRenderOption extends IRenderOption {
public static final String OUTPUT_FORMAT_JSON = "json";
public static final String OUTPUT_EMITTERID_JSON = "org.eclipse.birt.report.engine.emitter.json";
}
JsonRenderOption:
package org.eclipse.birt.report.engine.emitter.json;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.RenderOption;
public class JsonRenderOption extends RenderOption implements IJsonRenderOption {
public JsonRenderOption() {
super();
}
public JsonRenderOption(IRenderOption options) {
super(options);
}
}
I hope somebody can help me
Greetz
Try importing the following packages
importPackage(org.eclipse.birt.report.engine.emitter.json);
importPackage(org.eclipse.birt.report.engine.api.IRenderOption);
importPackage(org.eclipse.birt.report.engine.api.RenderOption);
Hi i am new in java reflection domain.So can anyone guide me in this problem scenario.
I have a class named "SomClass.java" and it imports a package named "SomPackage.RefClass" And some other java libraries like java.lang.. etc.
Now i wish to get know all the imports defined in a class through reflection.
import SomPackage.RefClass;
import java.lang.reflect.Field;
import java.io.IOException;
public class SomeClass{
RefClass refClass_Obj;
String nationality;
///some other members
}
I just wanna know the list of all import defined in a class using reflection.
I have seen a Question posted hear similar to my Q but it is not well elaborated so,need some good direction of help.
thanks in advance.
I just wanna know the list of all
import defined in a class using
reflection
You can't because the compiler doesn't put them into the object file. It throws them away. Import is just a shorthand to the compiler.
Imports are a compile-time feature - there's no difference to the compiled code between a version which uses the full name of the type everywhere it's mentioned, a version which imports everything using a *, and a version which imports classes by full name.
If you want to find all the types used within the compiled code, that's a slightly different matter. You may want to look at BCEL as a way of analyzing bytecode.
I think you can use Qdox to get all the imports in a class which is not actually through reflection, but it can serve your purpose :
String fileFullPath = "Your\\java\\ file \\full\\path";
JavaDocBuilder builder = new JavaDocBuilder();
builder.addSource(new FileReader( fileFullPath ));
JavaSource src = builder.getSources()[0];
String[] imports = src.getImports();
for ( String imp : imports )
{
System.out.println(imp);
}
As suggested by #Asraful Haque qdox helps to read imports of java file
Use Maven dependency
<dependency>
<groupId>com.thoughtworks.qdox</groupId>
<artifactId>qdox</artifactId>
<version>2.0.1</version>
</dependency>
Please refer modified version of code
package readimports;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Collection;
import java.util.List;
import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.JavaSource;
public class TestReadAllImport {
public static void main(String[] args) throws FileNotFoundException {
String fileFullPath = "path to java file";
JavaProjectBuilder builder = new JavaProjectBuilder();
builder.addSource(new FileReader( fileFullPath ));
Collection<JavaSource> srcs = builder.getSources();
for(JavaSource src : srcs) {
List<String> imports = src.getImports();
for ( String imp : imports )
{
System.out.println(imp);
}
}
}
}
Thanks for sharing the qdox, I used it to recursively find all imports and find unique packages and unique imports.
<!-- https://mvnrepository.com/artifact/com.thoughtworks.qdox/qdox -->
<dependency>
<groupId>com.thoughtworks.qdox</groupId>
<artifactId>qdox</artifactId>
<version>2.0.3</version>
</dependency>
Using simple recursion to get all packages and imports of the given class.
package test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.JavaSource;
public class ImportsIdentifier {
private static String sysPath ="//<Absolute Path>/src/main/java/";
private static String fileType = ".java";
private static Set<String> importFiles = new HashSet<>();
private static Set<String> packages = new HashSet<>();
public static void main(String[] args) throws FileNotFoundException {
String path = sysPath + "<java file path>";
printImports(path);
System.out.println(importFiles);
System.out.println(packages);
}
private static void printImports(String path) throws FileNotFoundException {
JavaProjectBuilder jp = new JavaProjectBuilder();
jp.addSource(new FileReader(path));
Collection<JavaSource> srcs = jp.getSources();
for (JavaSource src : srcs) {
System.out.println(src.getPackage());
packages.add(src.getPackage().toString());
for(String imprt: src.getImports()) {
if(imprt.startsWith("<filter for any package>")) {
imprt = sysPath+imprt.replaceAll("\\.", "/")+fileType;
if(importFiles.contains(imprt)) {
continue;
}
importFiles.add(imprt);
System.out.println(imprt);
printImports(imprt);
}
}
}
}
}