Running Java file in IntelliJ - java

I need some help with IntelliJ. I am fairly new to programming, and I have some experience with the IDE but I am by no means a know-it-all when it comes to IntelliJ.
I have 3 Java files (without their respective class files). When I open intelliJ it asks me:
Create New Project
Import Project
Open
Check out from Version Control
I click on open, IntelliJ opens up a file location, I click on one of the three Java files to open, and I drag the other two one by one (they open in their separate tab).
I cannot figure out how to run them. It is likely that I need to create run configuration and I don't know how to do it. I know how to create a new project and get everything working, but I don't know how I can get the files working if I just open them. Can someone tell me how to create the run configurations to run the files? Please and thank you.

IntelliJ IDEA is a great IDE!
This is what I would do:
Select File/New/Project... in the menu. You will get a new modal window.
Then select Java on the left, make sure that you have your JDK selected (top right), it not there, create it right from the window. Click 'Next'.
Just go 'Next' on the 'create project from template' step.
Then type your project name and select a folder for it. It will create a new project.
After that, copy your java files into /src folder using drag-and-drop in IDEA or just a file manager you like.
Now you have an IDEA project with your source code in it. You might need to sync your IDEA project, use File/Synchronize for that.
Finally, you can right click on you java file and 'Run' it from IDEA.
It the project fails on compilation you might need to add required imports and libraries, but that depends on your code. IDEA will highlight all the errors.

Related

IntelliJ Idea unable to execute java Program

I am trying to learn Java from an online class, however I am having problems following the instructor's video.
is his screen, where he is trying to print a line.
My screen looks like
As you can see, the "out" part of System.out is not the same on our screens, and the icon for class is different on both our screens as well. The main problem I am having is I'm not able to run the line of code that I wrote. I apologize if this question has been asked already, I was not sure what keywords I would use to search this problem.
As you can see, the "out" part of System.out is not the same on our screens
This probably is due to the fact that you haven't opened up a Java Project. As you can probably see in the Two Images, the instructor's image, has A Project hierarchy on the left, while yours is empty. Therefore, To correct this Try The Following Steps:
File->New Project->Java Project
Now follow the onscreen instructions to create the Java Project. Then the project would be created. Now expand the project on the left pane, and do it until you find a folder named src . Select the folder, and choose new Class, and then You can create your new Classes
The main problem I am having is I'm not able to run the line of code that I wrote.
This is probably due to the fact that you Have not set up your JDK path in IntellIJ. This is backed up by the fact, that your run button (The Green Arrow on the Instructor's), is grayed. To fix this, click On the small Button beside the arrow. There, Click On edit Configurations. The following Window should pop up
If Default is not expanded, then expand it and Choose Application. The Window Should probably be like this :-
Then Click On the plus icon on the top
There, Enter You Main Class(The Class Which has public static main void(String args[]))
If you can't, choose the ... option, and choose
Finally, select the JRE path, that is, the location where you installed The JRE.
Also, make sure that You have the 'path' environment variable set to the bin of your jdk Installation Folder.
Ask if you have any problems doing this.
First you shoud do this: File- New- Project... Then create new project. And then you click right mouse button on src folder(in your new project), choose new "Java class" and copy code to that new class. And then you'll be able to run your main method.
Problem is that you just use java-file from somewhere (desktop, probably), but you should have it inside your project.

Intelli-J: cannot create class-file?

This is honestly a tiny problem but it's keeping me from proceeding with my small Java practice app.
I'm currently practicing making an API call in Java. I was trying to create a simple class file called "Film". However, when I try to create it as a class-file, Intelli-J keeps telling me that it is unable to create a class-file. I'm trying to set this file up in a folder called "models". All of these class files are going to be utilized as part of an app that performs API calls.
Is there some naming convention I haven't followed? I've been Googling but haven't really found anything.
More directly, based on what #Vishal Jumani touched on, you need to tell IntelliJ what directories are 'source' or 'test' directories.
A directory structure you can use as an example, but you can use whatever you wish.
Right click on the directory you wish to mark as 'root' --> find 'Mark Directory As' --> Select 'Sources Root'
Now you should be able to add java classes in the directory without IntelliJ interrupting you!
This would usually be because of the way your project is setup. It may not be setup correctly to indicate where your source code is.
To setup your project, in the Project Tab, click on the top most folder and select F4. This should bring up the Project Settings dialog window.
Now click on Modules in the LHS, and then select the Sources Tab on the RHS
Select your src folder and click on the Sources button, to indicate this is your source folder. Now IntelliJ is aware that this is where your code is.
Click on Apply and OK. This should close the Dialog Window.
Now right click on the src folder and then select New -> Java Class to create your Java class

Cannot get JavaFX to work in Eclipse

I'm trying to use JavaFX in Eclipse but whenever I use any of it's Classes it doesn't recognize the type. To clarify I get the message "Dropshadow cannot be resolved to a type"
This happens not only with Dropshadow, but with any of the JavaFX classes. The IDE won't give me an option to import it and when I try and to type it in manually it isn't recognized. In using JSE 8u25, which apparently has it included.
In looking around I see that the .jar file can be added manually, but I would like to know if something within my IDE isn't configured right, because it should be working. Can anybody give me a hand with this? Thanks.
So I finally got it working, the solution was to install the eclipse plugin found here:
http://www.eclipse.org/efxclipse/install.html
The directions are pretty good, I just followed them as written and got it up and running.
Adding to Build Path
Your problem here, is that you just dropped your .jar file right in there, and tried using it. You need to add it to the project. To do so, follow the below steps:
Add the .jar file to your project folder (not src) via. copy/paste or simply drag/drop.
Right click on the .jar file once it's inside your project.
Press Build Path > Add To Build Path
What you have/will done, is adding the .jar to the project itself. Theres a difference between having it in your project, and having it a part of your project.
Now you can import, extend, implement, whatever you need.
Removing from Build Path
Ok, so you found a better library for what you want, no longer need the library or made your own, and you want to delete your .jar.
Well, firstly, it's still a part of your project, so you need to remove it from the Build Path first.
Right click on the .jar file.
Press Build Path > Remove from Build Path
Your .jar is still inside your project, but no longer a part of it.
Now you may go ahead and delete it without worry.
The simple way to solve your issue is to right click your project, go to properties > java build path then select the tab where it says Libraries.
You have to select that library you have in there > click the button EDIT then select from there Workspace default JRE . You need to have at least java 8.

Eclipse Java Project - Open Declaration always opens class file instead of .java

I'm posting this query after doing lots of googling and trying out different options for last few days. Is there an easy way to attach source in Eclipse? is the closest answer I found to my question.
From above post, I tried all solutions but each time Eclipse is opening .class file instead of .java file.
Eclipse Details:
Kepler Service Release 2
Build id:20140224-0627
Please let me know if this is a bug with Eclipse or if I am missing something.
If you are using Maven, try this:
Double click on your project in eclipse -> Maven -> Enable Workspace Resolution
This is most likely because Eclipse actually use the class file for declaring the item you have requested the declaration for, and not the source file you think it should be using.
The typical reason for this is if you have a jar file containing previously compiled classes on your build path before (or instead) of the project containing the source you want. This is very rarely desirable as it can take quite a while to discover that your edits do not take effect.
Examine your build path carefully and ensure it is as you want it to be.
Maven is likely supplying a Classpath Container to your project, and generating entries for the Java Build Path based on the pom.xml contents (the JRE System Library is another example of this). Its entries will not have source attachment unless the container decides it will, which is why the Installed JREs preference page offers to let you set Source Attachments. If the pom.xml has something like that, see if you can use it. If not, see if you can configure the Maven container from its context menu. Otherwise, you may simply be stuck.
I had this same problem: when I pressed F3 or tried to open Declaration (after right click) for any element, it took me to the bytecode .class file.
My solution:
I went to the project in Project Explorer:
right click on project name->Properties
Then in Porject Properties window,
Java Build Path->"Order and Export" tab
There I selected the folder with the .java files (source code), which was below the folder having the .class files -in my case "JRE System Library [java-11-openjdk-amd64]", and pressed 'Top' button on the right frame. Then the folder with source files moved to the top.
Finally, pressed "Apply and Close" button.
That solved my problem forever and ever.
The folder
I installed a plug-in which allows me to download the sources for a maven project.
I'm not by my develop machine right now, but I think it was this:
http://marketplace.eclipse.org/content/m2e-dynamic-sources-lookup#.U8gjGWIaySM
With this, you can select a project an have maven download the sources and make then available when you navigate the code in Eclipse. All without "leaving" Eclipse.
Check into your project modules the Java Build Path, look inside the Source tab and verify if you have "Allow output folders for source folders" checkbox flagged; in this case you just need to uncheck it and you will be able to see again the right source from java files.
This is related to the Call Hierarchy in Eclipse IDE.
If you are in a situation, where you're seeing duplicate methods inside the call hierarchy showing Members calling 'myMethod(...)', double-clicking on one of the listed methods may open the .class file instead of the .java file.
When this happens, the solution is to set the search scope in the call hierarchy to Project instead of Workspace.
After setting the search scope, you won't be seeing duplicate methods listed in the call hierarchy, and double-clicking on the listed methods will only open the .java files.
(Using Eclipse DevStyle Dark Theme)

Exporting a SWT Project in Eclipse (Project Setup via SWT Designer)

I made a small application with java in Eclipse using SWT for my GUI. I set up the project by installing SWT Designer, which is very similar to Swing Designer only for SWT. I created the project by going to File >> New >> Other >> Window Builder >> SWT Designer >> SWT/JFace Java Project. This set up the whole project for me so I could start using SWT. My program successfully runs when I use Eclipse to run it.
I want to be able to run it without Eclipse, though. I tried to export it as a .jar by going File >> Export >> Java >> JAR file. I selected my project, hit Next >> Next, then I set my main class and hit finish. No errors.
Opening the .jar gave me:
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Layout
It was pretty clear that SWT isn't where it should be, so I opened the .jar and found only my files, the META-INF, etc. No SWT anything. No jar, no org folder, nada.
In my Eclipse project, under the Project Explorer, it lists "Referenced Libraries", where the SWT stuff is set up. When looking under the properties of it, it lists a bunch of org.eclipse... stuff. I assume this is how Eclipse knows what to run, but it seems to not export it.
So my question is: How do I get the project to export with SWT packaged with it, in order to be able to run by the .jar alone? It would be nice if you could explain both the manual way (i.e. editing the META-INF and dragging and dropping the required SWT files) and also explain the Eclipse export way (so I don't have to do the manual way all the time), if there is a way to do it in Eclipse.
EDIT: If you see my answer below, I explained how I was able to get this working, however, it dumps ~16MB worth of code into my .jar, which is pretty big for an originally < 1MB project. If you have any idea on how to make it only add the used files, an answer is still appreciated. ;)
Does not sounds like you are using OSGI, my suggestion would be to go to your eclipse plugin directory, grab the swt jar copy it into your plugin and add it to the classpath by going to manifest editor, runtime tab and selecting the jar. If you are running an osgi app, make sure your .product file has the correct dependencies by going to .product editor dependencies tab and selected add required plugins. If you are running this in a different context comment and I will answer.
The way I successfully made this work, was by exporting it as a runnable JAR.
My steps to getting it working:
Run the main class. (I specifically use CTRL+F11, the default run on the class, letting Eclipse guess how it's supposed to be ran.)
Go to File >> Export >> Runnable JAR.
Select my Launch Configuration. (It's listed when I hit the arrow beside the run button on the top bar on Juno.)
Hit the finish key and wait!

Categories

Resources