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.
Related
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.
I'm a Java beginner and came from JGrasp where I could simply open up a class anywhere and be able to run it without any problems. When I open a class in Intellij it says the configuration isn't correct. Is there a way to set up Intellij by default to automatically set up the SDK for everything to default and allow me to open up any class without setting up configuration for each one. I attached an image that shows the edit configuration popup for a directory I opened. I am aware how to set up a project from scratch but I'd like to be able to open up code from anywhere without too much hassle setting up SDKs and configurations every time.
Here is the code :
To be able to run a class, you first need that class to have a "psvm" (public static void main, which can be easily generated for you by simply writing "psvm" when you're coding a new method in a class).
Once that's done, you head over to the left, in your project's folder-tree, right-click your class, and press the "Run" button.
The green arrow on top of the class' blue circle indicates that this class has a "psvm" and that you thus can "Run" it.
After having done this once, IntelliJ will remember that you have ran this class and the "Edit confirguration" dropdown should usually always allow you to select it. Once the class you want to Run from that dropdown is selected, you can simply press the green triangle next to it.
You should place the source code (that is file with .java extension) to a separate directory (from the compiled classes (that is file with .class extension) and set it as a source root in IDEA: right click on the folder which server as a root package directory and select -> Mark As -> Source Root.
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
I'm not entirely sure how to word this but how would I go about changing which java file in my package is the first one to open when the compiled jar file is executed?
I'm also using netbeans if that helps.
You can changed it in Netbeans. When you run the whole project in netbeans, the first class that is executed there is also the first class to be executed in the jar file built by Netbeans.
You can right click your project in the project window and select Properties. From the window that opens, select the run category in the left side. Then on the right side, there is a button to browse and select the file for the Main Class.
The manifest indicates the Main-Class that runs when you execute a jar file.
https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
If you have two (or more) classes with public static void main, click File | Project properties and in the Categories dialog, click Run and look for Main Class:. and then click Browse... to select which main class to start with.
(But I'm not sure this answers what you really want to do since once compiled with the chosen main class, there's no way to later have the .jar file run another class. If this doesn't answer your question, maybe a good question in return is, "What are you trying to do?")
When I run an application into eclipse, if an exception is thrown and is showed in the console, then you are able to click on the links which takes you to the source code file.
Fine, the problem is that when I click those links they open as external uneditable resources (different icon in the editor tab, Java source files have a blue J but these files have the J empty, only showing the border). Same problem when debugging, the files opened are not the same tabs as the ones edited.
For more information, while the normal source files shows in the title bar something like:
project/path/to/source/file.java
The others show its complete path:
C:\absolut\path\to\source\file.java
This is quite annoying since I have to go and back from one to the other and I have no idea where to change this or why it is happening (it was alright a time ago).
Any hints on how to edit this?
Thanks
It is a classpath problem. When you click, you are actually accessing the file from the classpath.
To view the Java file, you have to add a reference to your project in the classpath and move it to the top of the classpath list.
You can also try converting it to project facets java version, right click on project --> properties --> choose Project Facets -->java 1.7 or [your version]. Apply.