JavaFX won't import in BlueJ on Mac - java

I'm programming a music player in BlueJ for a school project, and I want to make use of a few classes from the JavaFX library. However, when I try to import (see import statements below), I get an error saying "javafx.scene.media does not exist". I have downloaded the latest JDK.
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
It should be noted that since it is a school project, I am obliged to use BlueJ, so switching to another IDE (in case that might be where the problem lies) is not an option.
Thanks in advance!

At present you'll need to add the JavaFX jar manually as a library - in the future this may change, but for now, go to Tools => Preferences, click on the "Libraries" tab, click "Add", then locate jfxrt.jar - it should be in your jdk directory, under jre/lib. When you're done, you'll get something like the following:
Reset your VM (Ctrl+shift+R) and then your import statements should work properly.

Related

Eclipse cannot import import com.google.maps.* (For java Fx programme)

I am working on a Java fx application and I need to import some libraries from com.google.maps.
I imported these libraries :
import com.google.maps.GeoApiContext;
import com.google.maps.PlaceDetailsRequest;
import com.google.maps.PlacesApi;
import com.google.maps.QueryAutocompleteRequest;
import com.google.maps.errors.ApiException;
import com.google.maps.model.AddressComponent;
import com.google.maps.model.AddressComponentType;
import com.google.maps.model.AutocompletePrediction;
import com.google.maps.model.PlaceDetails;
The java import statement is a little bit misnamed, it really means alias. import com.google.maps.GeoApiContext; really just means: Any time you find the type GeoApiContext anywhere in this source file, assume I meant to write com.google.maps.GeoApiContext.
Crucially, it does not 'invoke' any code in that class whatsoever, nor does it find or download any dependencies from the internet for you.
You will need to find the jar(s) that provide these classes and put them on the classpath of this project.
It can be as simple as downloading the relevant jar (perhaps com.google.maps-google-maps-services.jar?), put it in a lib dir someplace inside this project, finding that in the package explorer, right clicking it, and selecting 'add to classpath'.
Or, more likely, you want to use gradle or maven to take care of this for you: These tools turn simply mentioning the dependency in a list of libraries you require into automatically finding that on the internet, downloading it, configuring your IDE so that it knows where it is, and using that dependency during build and run steps.

Intellij class Import being added to object not the top of class

I've just started a new IntelliJ project. When I try and import a java class with the IntelliJ import shortcut, the import is being added to the object and not to the top of the class like it normally would.
Where in the settings can I change this?
See Preferences/Settings > Editor > Code Style > Java if this option ...
Use fully qualified class names
... is ticked then IntelliJ will always use fully qualified class names. If you disable this option then Intellij will include the import statement and refer to the class by its 'simple' name.
Here's a screenshot showing this configuration item:
If the accepted answer is not working for anyone, Try the below points too.
Check whether the class file is in correct package and resolve if there is any issues in the class file.
Click File -> Invalidate Caches/ Restart and Click Invalidate and Restart button, Once it restarted U'll be able to import properly.
I was facing this issue with my custom / generated(protobuf) java packages.
Solution (TDLR)
Whenever you face this issue, just check the package or class that getting imported doesn't have any error(red marking).
If so try to fix that.
I was having an issue with package naming and its actual location. By generated-sources -> Right Click -> Mark Directory As -> Sources Root corrected my issue.
Check whether generated-sources or folder under the generated sources need to mark us Sources Root.
My issue was with generated java files (Adding this just to help someone else with same issue)
Intellij shows red mark for Import with wildchar(import pkg1.subpkg1.*;). Like editor was unable to recognizing 'import all' under a package.
But direct import to the inner package was fine.
On top of that an resolving issue with import class add full package to the Class itself, instead of adding import statement on top the java file.
This issue didn't affect the code compilation, only in editor it was showing red mark.

Unable to import gpdraw in Java

I am unable to import the class gpdraw in java.
import gpdraw.*;
This is the import statement I used. I double and triple checked it - it's definitely correct- but it keeps throwing the exception package gpdraw does not exist. Can anyone think of why?
I use Bluej on a Macbook, if that helps.
The problem is that gpdraw is not included in Java per default. As MadProgrammer pointed out, you have to include the library in your compile/runtime classpath.
BlueJ:
The first way is via the "Preferences" dialog. Open the "Preferences" dialogue and select the "Libraries" tab. Then add the location where your classes are as a library path. Restart BlueJ - done. The selected libraries will now be available in all projects that you open.
(http://www.bluej.org/faq.html)
Command:
javac -sourcepath src -classpath path/to/gpdraw.jar
(http://ubuntuforums.org/showthread.php?t=637270)

Package com.google does not exist

I'm using Netbeans IDE 7.0.1.
I base my question on the CalculatorApp sample application available with the IDE.
The sample is running fine but I wish I could use some JSON, ad so I download google-gson-stream-1.7.1.jar library, and included it in my libraries :
- right click on "Library", then "Properties" then "Add JAR/Folder" and selected my JAR (located somewhere in "My Documents/Download".
The problem is that when i type the following :
import com.google.gson;
(or even com.google), the IDE fires a "Package com.google does not exist". The JAR and its hierarchy is correctly shown on the left side pane,in the "Libraries" section. Most surprising, the IDE can autocomplete the com.google.** stuff.
AFAIK, I need to further to get this work, don't I ? Any ideas ?
Thank you !
In Java, import statements are for classes and interfaces (types) not for packages themselves. Are you sure you tried to import a class under com.google.gson package and not the package itself?
import com.google.gson.Foo; should work.
So should
import com.google.gson.*; or
import com.google.*;
Note however that wildcard imports aren't particularly recommended.
I am using:
import com.google.gson.Gson;

HTTPCLIENT does not exist? Netbeans

I am trying to import:
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
But I am being told these do not exist?
I downloaded:
httpclient-4.0.1.jar and httpmime-4.0.1.jar
... and placed these in the same folder as my .java files that are trying to use httpclient.
Any ideas?
I still cannot get it to work... Within the folder "Libraries" I have: apache-mime4j0.6.jar commons-codec-1.3.jar commons-logging-1.1.1.jar httpclient-4.0.1.jar httpcore-4.0.1.jar httpmime-4.0.1.jar For the java file properties it has: compile classpath runtime classpath boot classpath In each of those, it seems to refer to the jars I have imported. Still getting does not exist. :-(
I have tried to do this in Eclipse too and now those files appear in "Referenced libraries" however it still doesn't work. lol
The two jars you have mentioned need to be placed in the classpath of the project in Netbeans, not in the source directory.
In my Netbeans 6.7.1 on Mac, in the Prjects tab, you cna right click on the project and select Properties. That will bring up the project properties dialog. In there, choose the libraries item from the tree on the left. From there, choose the Add Jar/Folder in the Compile view. To add the jar to your project, use the chooser to locate it and then select it.
EDIT:
I have just downloaded the HTTPClient package and I think I see the problem:
in 4.0.1, the package structure is not as you have it defined. Instead of:
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
use:
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.client.params.HttpMethodParams;
In Eclipse, press Ctrl + Shift + O to organize your imports. This will look for all unknown classes on the classpath and try to import them. You can also place your cursor on a class name and press Ctrl + Shift + M to attempt to import that single class. This is sometimes helpful for class name collision (i.e. if two packages have a HttpClient class, you can click on the desired class).
If the jars are in Referenced Libraries, then they should be on your classpath. You can verify this by right clicking the project and selecting something like Build Path > Configure Build Path, then click the libraries tab.
Also, you probably have build automatically selected by default, but if you don't, you'll need to build your project. You may also want to attempt to clear the build path and re-build it. I've seen my Eclipse get out of synch a few times and this fixed it, albeit somewhat of a fluke.
If you're using Maven, this sort of thing can sometimes occur if you have an incorrect dependency scope (i.e. runtime, or test vs. compile).
For what it's worth, unless you're utilizing the entire package, there is no reason to import an entire package's contents (i.e. import package.*).
It seems that HttpClient has changed his sintaxis from 3 to 4 version... I had same problems that all of you trying to import packages until I found this example:
http://w3mentor.com/learn/java/android-development/android-http-services/example-of-http-get-request-using-httpclient-in-android/
This is sample is Android oriented but works on any Java Application!!! Im using netbeans 6.9.1, httpclient-4.1.1.jar, commons-codec-1.4.jar and commons-logging-1.1.1.jar
Hope you can solve your problems!!!
Had the same problem and i managed to get the solution. Here it is:
1) Download the org.apache.commons.httpclient.jar.zip file from
http://www.java2s.com/Code/Jar/o/Downloadorgapachecommonshttpclientjar.htm
and save it anywhere on your computer.
2) Right click on your NetBeans project and select Properties
3) On Project Properties Categories, select Libraries
4) Click the Add JAR/Folder button
5) Now browse to the file location where you saved your downloaded org.apache....jar.zip
file and click open button.
6) Now the file has been installed, click OK and you are done.
Note that this might require you to restart your netbeans IDE.

Categories