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.
Related
I wrote a program that spans a few classes in IntelliJ and it works fine when I was testing it in the IDE. However, whenever I follow tutorials to make my project into a .jar executable, it does not run.
The file in the out folder does not run when double-click on it and says "The Java JAR file "projectName.jar" could not be launched.
When opening it from the terminal, I get the error "Could not find or load main class". I used "$ java -jar projectName.jar "
I followed these steps:
Open Project Structure, Artifacts and click the (+).
Under JAR, from modules with dependencies.
Point to the class with my main method and click OK.
Click apply and OK to exit.
Then I close project structure and open Build and then Build Artifacts...
Then I click Build.
Navigate to ~/IdeaProjects/projectName/out/artifacts/projectName_jar/projectName.jar
Then I would try to run it but it does not run. My project has both a GUI and can be run from command lines. When I compiled the main method in Terminal, it worked fine, but I need a JAR file. I'm not sure what I should include in the question, since I'm sure that my code is unrelated. I also utilize Maven, not sure if that is related. Are there other things that I should be doing or adding to my project before I build the JAR?
This can be a result of the Manifest being created in the wrong place.
First make sure it is in src/main/resources/META-INF/MANIFEST.MF
(Intellij sometimes incorrectly places it in /src/main/java/META-INF/MANIFEST.MF)
Then Make sure the jar is looking for META-INF in the src dir NOT in /java OR /resources. This means:
When you set up File>Project Structure>Artifacts>+>JAR>From modules with dependencies... then your Directory for META-INF/MANIFEST.MF should end at src. Example:
ONLY C:\Users\User\StudioProjects\Project\src\
NOT THE PREPOPULATED C:\Users\User\StudioProjects\Project\src\main\java
If your Project Structure jar is already set up change the Manifest File path to:
C:\Users\User\StudioProjects\Project\src\META-INF\MANIFEST.MF
INSTEAD OF C:\Users\User\StudioProjects\Project\src\main\java\META-INF\MANIFEST.MF
You need to make sure that the Main-Class attribute is set. One way to check this is by opening the Jar with a Zip utility and looking for META-MF/MANIFEST.MF extract or open that file with a notepad, notepad++ or any document reader. If "Main-Class" is not in their or set to the correct main class you will received the error you have received. Seeing the IntelliJ tutorial it has a Main-Class field when setting up artifact publishing. I have included the link below to the IntelliJ page.
IntelliJ Creating an Artifact Tutorial
I have converted my java code into .jar now i want to use the class in that jar file in another class, so i am unable to do so. Thank you very much for any kind of help!
In Eclipse, right click on your project where you want to use your jar. Select "Properties", there select "Java Build Path". Under "Libraries" you can add the JAR (if you copied it to a lib folder in your project), or select an external JAR (if you don't have it in your lib folder).
When this is done, you can use it in your code. Just use the class, and when a red scribble is underlining your class name, hover over it and select "Import 'Classname' (this.is.your.package)". Alternately, you can let Eclipse auto import the most logical package by pressing "CTRL+Shift+O" (hotkey to auto import)
Add the jar in the new project classpath and then you should be able to use the classes.
I have an eclipse project that contains several classes and packages. I want to create a runnable jar of just one class from the project. But this jar should contain all the dependencies too. Can someone please tell me how can I do this? Most posts on StackOverflow have answers for creating a jar file for the entire project, but I only need jar of one class with all dependencies bundled together.
I could finally figure out how to do it. Just right click on your class file for which you'd like to create a jar. Select Export --> Jar File. Click on next. In select resources to export, select the project name. Expand the project to find the package that contains the class you'd like to export as a jar. Select the class name. Check the box --> Export generated class files and resources. Select the export destination, and check the option --> Compress the contents of the JAR file. Click on next. Under select options for handling problems, leave both the options checked. You don't need to check the option - Save the description of this JAR. Click on next. Here, leave the default options. Under select the class of the application entry point, specify your class (Make sure your class has a main method). And finally click on finish. Once this is done, go to the path where you've saved the jar. Right click, and say open with --> Archive utility. This will create a folder of your jar file. In my case, I named the folder lib. Inside this folder, copy all the JARs that this class needs to execute.
Finally, do this from the folder where you have the folder of your jar file --
myMac$ JAVA_HOME=`/usr/libexec/java_home`
myMac$ libraries=`ls lib/*.jar | tr "\n" ":"`
myMac$ $JAVA_HOME/bin/java -cp $libraries com.test.rick.GeoData
GeoData here is the class which I was trying to export as a JAR. Hope this is useful for someone in the future.
I want to create a jar file in Netbeans using the clean and build options. The jar file is created, but the manifest file is not updating; this means the main class is not set in the manifest file, so when I run the jar file, it has the "could not load main class" error.
How do I handle this and how do I add main class in project properties?
Right click on netbeans project and select "Properties"
Then select "Run". There is option for selecting main class. Select main class from there.Then click "ok".
After doing this try run your project at least once before building your jar file.
Hope You did not set up main class in project setup.
So do right click on project.
then go to properties.
On that screen you can see Run configuration.
There is place to configure main class of you jar file.
So configure your main class at there and press ok.
Then clean and rebuild the project.
After that manifest file should update.
May be you are running your project using shift+F6 key. You should run your project once by pressing F6 key only then the netbeans gives option to select main class then after that clean and build.
I'm new to java with a C++ background. I'm using Eclipse and have imported projects created by maven using "mvn eclipse:eclipse". In eclipse, when I highlight a class name from another class and do "Open Declaration", eclipse would normally open the corresponding .java file.
However, for one of the classes, eclipse "Open Declaration" opens up a .class file which shows all the context of the file. However, this file is not editable. Furthermore, I can't locate the actual .java file behind it. There is a package line in the beginning of the .class file and it points to a directory that doesn't seem exist. I believe there is a way to trace back to the original .java file from the .class file but I'm not sure how to do this. Please help.
The class is likely in one of your dependency jars, so you don't have the source locally in a project. Many maven published projects allow for the source to be downloaded in a jar, and eclipse can use that to display the source, but editing it would be meaningless in that instance.
If you hit ctrl+shift+T and search for the class you should be able to see the location of the class in the bottom portion of the "open type" dialog.
In the eclipse "properties" for the jar that contains the class, you should be able to set the "Java Source Attachment" to the folder on your system that contains the source (if you have it)