Skip to main content

Using external java class in JavaFX

11 replies [Last post]
ic2
Offline
Joined: 2008-05-17
Points: 0

Hello everybody.
I have such kind of problem.
------------------------------------------------a.java--------------------------------------------------------------------
public class a{
public a(){
do smth
}
public int count(){
int number;
do smth
return number;
}
}
---------------------------------------------b.fx-------------------------------------------------------------------------
package b;

var aInstance=new a();
................................
do smth
...............................
---------------------------------------------------------------------------------------------------------------------------
Both of files are in the same package.
There is a problem in b.fx:
cannot find symbol
symbol : class a
Can anybody explain me what i do in not a proper way and how to use external Java classes from JavaFX.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tengimon
Offline
Joined: 2008-06-23
Points: 0

hey but this things still doesnt work. i made the exactly same code, and it still reports errors.

here is my code.
----------------------------java class----------------------------------
package testing;

public class myJavaClass {
public int x;
public myJavaClass(){
x=10;
}
public int count(){
int number = 0;
number = x+number;
return number;
}
}

---------------------------------main.fx file------------------------------------
package testing;

import testing.*;

var instance = new myJavaClass(); //<--------------here it shows the error

error message: Cannot find symbol
symbol: class myJavaClass
location: testing.Main

Is there anything that i am missing?
I used netbeans 6.1. javafx update 2008-06-30_13-09-27.zip on jdk 6. Can you tell me more details about which version you are using and what can be different than yours?

Thanks

alexsch
Offline
Joined: 2007-07-12
Points: 0

I use the last version of JavaFX Script plugin (2008-07-04_02-32-47.zip) and JDK 6u5.
It works for me and should definitely work for you.

Do you build all your project?

tengimon
Offline
Joined: 2008-06-23
Points: 0

yeah i do build them...btw thanks a lot though, now it is working. i think that my problem was my jdk which wasn't the latest. also Netbeans has some problems with importing java class. it shows error on the screen and doesn't run but after building or restarting it can actually run and at the same time, it still shows errors on the screen.

zizul
Offline
Joined: 2008-06-01
Points: 0

And what if I have my java class in other package than javafx class ?
I can't get it work, always gives me [b]package v.x does not exist[/b] when compiling javafx class.
I tried adding path to java class while compiling: [b]javafxc -classpath ... b.fx[/b], but it didn't help.

----------a.java ------------------------

package v.x;

public class a {
public a(){
//do smth
}
public int count(){
int number = 0;
//do smth
return number;
}
}

---------------------------- b.fx---------------
package v.y;
import v.x.*;
var aInstance = new a();[i][/i][i][/i]

shagus
Offline
Joined: 2007-05-10
Points: 0

Hi, it works when java and javafx files are in different packages.

//-----------------------a.java ------------------------

package mypackage;

public class a {
public a(){
//do smth
}
public int count(){
int number = 0;
//do smth
return number;
}
}

//-------------------------- b.fx---------------
package yourpackage;
import mypackage.*;
var aInstance = new a();

-----------Compile a.java -------
javac -d . a.java

----------Compile b.fx -----------
javafxc -d . b.fx

--------Run b.fx ---------
javafx yourpackage.b

P.S. Check your classpath environment variable. Make sure it points to the current directory.
Add .; to the classpath environment variable.

zizul
Offline
Joined: 2008-06-01
Points: 0

I really don't understand how you could get it work.
I have everything the same way, and I still get error "package mypackage does not exist" error, when compiling b.fx file.
The classpath is ok. Any other ideas ?

shagus
Offline
Joined: 2007-05-10
Points: 0

Hi, here is my classpath environment variable.
.;C:\ora10g_dev\jdbc\lib;C:\Program Files\Sun\JavaDB\lib\derby.jar;C:\Program Files\Sun\JavaDB\lib\derbytools.jar;
It starts with .;

Could yuu show your classpath environment variable please?

zizul
Offline
Joined: 2008-06-01
Points: 0

Here it is:
.;C:\Program Files\Java\jre1.6.0_02\lib\ext\QTJava.zip;

shagus
Offline
Joined: 2007-05-10
Points: 0

Hi, Change the package name. Your class name and package name are the same.

Here is the working exampe:

----------a.java ------------------------

package v;

public class a {
public a(){
//do smth
}
public int count(){
int number = 0;
//do smth
return number;
}
}

---------------------------- b.fx---------------
package v;
import v.*;
var aInstance = new a();

-----------Compile a.java -------
javac -d . a.java

----------Compile b.fx -----------
javafxc -d . b.fx

--------Run b.fx ---------
javafx v.b

ic2
Offline
Joined: 2008-05-17
Points: 0

Thanks a lot :)

alexsch
Offline
Joined: 2007-07-12
Points: 0

The 'b.fx' file contains 'package b;' line. So you need to put the 'b.fx' file into 'b' directory
or remove the 'package b;' line from the file.

It is better to put the files into one package, for example 'pack' and add the 'package pack;' line into each file.