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?")
Related
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.
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.
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 trying to add another class to an IntelliJ project I created. I already have one program in the project and it is running perfectly. I tried to add another program to the same project's src folder but cannot Run this file. The s/w allows me to build and compile the file but I'm only able to Run the first program I created in the project.
I could do this in Pycharm but not in IntelliJ. Am I missing something? Is there a different method to achieve this in IntelliJ?
You should press shortcut :Alt+Shift+F10.Then you could choose which program you wanna run.
If you click on main method not found, it should bring you to the page where you can add the configuration.
I've never used Eclipse before am trying to import a .jar file and then run it without any command-line arguments.
I think the .jar file has been imported correctly, it is sitting in a created lib folder and I can see it in referenced libraries.
The .jar will not run because of the error "selection does not contain a main type".
I can see there is a main class inside the .jar when I extract it, but the manifest file does not include a 'main-class' specification. Because this is an assignment though, I cannot alter the .jar directory. Is there a way that I can run the file within Eclipse regardless of this?
Within the jar file all classes are part of a package. Would this alter the way I could try and run it?
To ensure, you imported the JAR correctly: In package explorer, right click your project, choose Properties, Java Build Path and tab Libraries. If your library doesn't appear, click button Add JARs or Add External JARs and choose your JAR file.
Then right click your project and choose Run As / Run Configurations.
In the dialog window click button New launch configuration. Then on the right side under Main class, input the full qualified class name of the main class which is contained in your JAR file. E.g. com.foo.Bar for class Bar in package com.foo. You can use the button Search to search for a main class.
Then click Apply and Run.
Write a class that will call the main class inside the jar.
Or simply ask your instructor.