I'm trying to set up IntelliJ IDEA -- tried 12 and 13 -- Mac to use a Java class included a jar file for my Java application.
I've set the jar as a module dependency by the following procedure
Start from the "project window";
Select my main Java module;
Use menu File | Project Structure;
In Project Settings, select Modules, and select my Java application as interested module in the middle window;
On the window to the right, select "Dependencies" tab, and press "+" and select "Jars or directories" and navigate to the jar file, add select it so the custom jar file is added as a new entry in the window, check the Export checkbox and set Scope pulldown set as "Compile";
Press OK.
However, after setting this, in a Java class file in my Java application, if I use a class defined in the custom jar file, it still prompts me "Cannot resolve symbol 'SomeClass'".
Any idea what I'm doing wrong and how to fix this?
Edit:
my settings:
BTW, the Libraries and Global libraries are both empty. I tried to set the two jars there (separately) and add them as dependencies for the Java app, and had no luck, so I deleted them from Libraries and Global libraries. The above setting is using add module dependency as files directly (without first registering them as libraries), which still does not work.
Putting everything in the default unnamed package solved the issue.
Related
i downloaded the JavaFX SDK from here but when i try adding it to IntelliJ i get the error "The selected directory is not a valid home for JDK".
[Project Structure -> Platform Settings -> SDK's -> add]
But i cant find an JDK from JavaFX, maybe i'am understanding something wrong between JDK's and SDK's. But IntelliJ says "add SDK" but need's JDK, can i switch it anywhere in the setting's or as i said above am i understanding something wrong.
Thanks in advance
Edit: The solution's beneth worked fine, here's what i found in the mean time link
IntelliJ doesn't make it all that easy to use JavaFX. I find the most intuitive way is to do the following:
Add global libraries for the JavaFX modules you want to use. The easiest way is to use from Maven and search for org.openjfx then add the modules you want (e.g. controls and fxml for the basic IntelliJ generated JavaFX project).
Create a module-info.java file for your app that looks something like the code below.
module MyModule {
requires javafx.controls;
requires javafx.fxml;
exports mypackage;
}
This creates a dependency on the javafx modules and exports your package so that the javafx reflection code has access to it.
There is an alternative method where the dependencies are added to the command line of the app's runtime but I find the method above more straightforward.
First, make sure that the JavaFX plugin is enabled (bundled and default). Then, in a new project, press Ctrl + Alt + Shift + s => Libraries => + => Java. Here, find the JavaFX folder you downloaded, then locate the lib folder in the JavaFX folder. Click on that, and it shouldn't complain about it not being a valid home. Apply your changes then close the dialog.
Now, in the main menu, hover over Run then click Edit configurations. Under the applications tab, click Main (or whatever your file is called). Then, next to the Build and Run section, hover over Modify options => Add VM options, then, in the text area, write the file path, paste:
--module-path /path/to/javafx/sdk --add-modules javafx.controls,javafx.fxml
Make sure you replace /path/to/javafx/sdk with the path to the lib folder in your JavaFX folder. If a name of a folder contains spaces, surround the entire file path with quotes.. Apply your changes and close the dialog.
Now you're done! Go ahead, code your JavaFX stuff, then hit run. Voila!
Official Documentation: https://www.jetbrains.com/help/idea/javafx.html
My JavaFx project is working properly in intellij idea. In edit configurations, I have chosen the main class as sample.Main and when clicking the run button of intellij idea, project works properly.
Then I have converted my JavaFX project to a .jar file using build artifacts. But when trying to run .jar by using java -jar project.jar, there is an error "Missing JavaFX application class sample.Main". I have used same class in both occasions. So why does it output error while running .jar?
Finally this worked for me.
File-->Project Structure-->Artifacts-->press + button -->Add JAR-->From modules with dependencies. Now there will apear interface called create JAR from modules. Choose your exact Main Class there. Choose option "copy to the output directory and link via manifest". Directory for META_INF/MANIFEST.MF usually takes to java directory. It is good if you choose resources directory. Then click OK. A new window will appear.Click Apply and OK.Now go to the tool bar and click Build-->Build Artifacts-->Build.You can find built artifacts under the "project compiler output path".If you don't know exact location for "project compiler output path",then go File-->Project Structure-->Project-->Project compiler output. At the end of the project compiler output path you can see "artifacts" folder.There you can find a folder which consists all built jars.Right click the folder and click the "Show in Files".You can go to the exact folder having your built jars easily. Then open the terminal there and run "java -jar project.jar".project must be replaced by your application name.
I know how to add .java/class files to a certain project, or import them into a package. So, when you're using eclipse you can import java.awt.; and java.util.; and the but those are all packages inside the JRE. So my real question is: is there any way to import a package containing the files i want into the JRE so that, if i create a new project, I can simply import them whenever I need them? Thanks!
Yes. Generally, you have more then one choice. You can link the project(s) in eclipse, and then have project B with a reference to project A. Or, you can create a build artifact like a jar file and add it to the project B build/run path. Simply add a jar file to your project, right click and select Add to Build Path. Another alternative is to use a build tool that manages dependencies like gradle, sbt, maven, or apache ivy. Note that there are a wide variety of third-party opensource libraries you can add as dependencies using these (and other) tools.
You seem to be asking how to create a shared library, which in Java is a .jar file. The steps to do this in Eclipse are:
In the Package Explorer, you can optionally pre-select one or more Java elements to export. (These will be automatically selected in the Opens the JAR export wizard JAR Package Specification wizard page, described in Step 4.)
Either from the context menu or from the menu bar's File menu, select Export.
Expand the Java node and select JAR file. Click Next.
In the JAR File Specification page, select the resources that you want to export in the Select the resources to export field.
Select the appropriate checkbox to specify whether you want to Export generated class files and resources or Export Java source files and resources. Note: Selected resources are exported in both cases.
In the Select the export destination field, either type or click Browse to select a location for the JAR file.
Select or clear the Compress the contents of the JAR file checkbox.
Select or clear the Overwrite existing files without warning checkbox. If you clear this checkbox, then you will be prompted to confirm the replacement of each file that will be overwritten.
Note: The overwrite option is applied when writing the JAR file, the JAR description, and the manifest file.
You have two options:
Click Finish to create the JAR file immediately.
Click Next to use the JAR Packaging Options page to set advanced options, create a JAR description, or change the default manifest.
Credit goes to the docs.
I have web application that is running in Jboss container.
Sometime I need to debug the code and for the I need to know from which jar current class is being referred (so that i could search of source jar of the corresponding jar)
Is it possible to get this Jar information (name of the jar file) from which current class is being referred. See the attached screen shot - from where i am trying to get this jar file information. Not sure if my approach is right or wrong
Eclipse allows you to make code changes during debug time.
So, if you want to entering in a class and know what is the jar source of this class just do CTRL + Mouse Right click in the class name and you enter inside the class.
If you have the "Link with Editor" option selected in your Package Explorer you can see automaticly what is ths jar file you are importing.
Compile time classpath settings are different from Runtime server classpath settings.
So if you want to know all the locations/jars that contain a particular class at compile time in eclipse then use Ctrl+T and type the class name. If there are multiple jar files with same class/namespace, then the order will be defined in Build Path settings --> Order and Export.
Runtime reference depends on class loading policy of your server configuration. But usually first it refers from local project, then manifest and then from common class path library.
If your server is configured to refer the workspace location of your project, then most probably the order would be as you defined in .classpath file, and you can use Ctrl+T to find it out.
Edit
It seems you want eclipse to resolve the source file automatically. If you are using maven then run below commands to refresh the source and javadoc for all your jar files.
mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc
A Visual Studio user struggling w/ Eclipse...
I imported a set of servlets/JSPs into a project in Oracle Workshop for Weblogic. /page.jsp has the following import statement:
import="com.foo.bar.*"
Eclipse is displaying an error:
The import com cannot be resolved.
The class that implements the above import is in /WEB-INF/src but I don't know how to build the class in Eclipse to resolve that error. I assumed Eclipse would automagically build the .java file and place the output in /WEB-INF/classes, but it's not doing that.
It could be that I haven't structured my project directories correctly so perhaps that's why Eclipse isn't building my source. Any suggestions? How can I get this to work?
First of all, /WEB-INF/src is a rather strange place to keep your java sources; you may want to move them out of /WEB-INF (into /src in project root, for example)
Either way, you need to tell Eclipse where your sources are and where you want classes built to. It's done in project properties dialog:
Right-click on your project in Eclipse, select Properties
Click on Java Build path on the left
Click source tab on the right
Click Add Folder button and add your source folder (/WEB-INF/src or wherever you moved it to)
Ensure Allow output folders for source folders is checked below
Under newly added source path select output folder and point it to /WEB-INF/classes or other location of your choice.