Location Required only in jar - java

I've been making a small java program to use and I am almost done with it. I tried exporting it as a runnable jar in both eclipse and IntelliJ but the jar is not running. I tried surrounding the launcher class in a try catch statement to see why and the exception.getMessage() simply says location required. I'm Using JavaFX for my GUI. Keep in mind that when I click run in both IDEs it runs perfectly.
I read a similar question here but it seemed that the person couldn't even run it in the IDE so please don't reference me back to it.
Note: I extracted the jar to see if my package containing my fxml file is in the jar and it is.
Thanks in advance

I had the same problem. Issue with mine was I was using relative path to get resources. For example
Parent addUser = FXMLLoader.load(getClass().getResource("../fxml/addUserGUI.fxml"));
I was using this, after changing it to
Parent addUser = FXMLLoader.load(getClass().getResource("/BikeShop/fxml/addUserGUI.fxml"));
It worked. BikeShop is my package name here, is the root directory of the project.

Related

Java: could not find or load main class main - not sure why

I am trying to upload a .jar file onto eclipse in order to be able to edit it.
The purpose of the .jar file is to track player coordinates on minecraft.
I have decompiled this .jar file on Eclipse, using the eclipse Java Decompiler.
However, when I try to run this decompiled code, I am getting the error: 'Could not find or load main class main'.
This happens when I try to run the 'decompiled code' by creating a run configuration and specify the main class: 'main'.
I have looked at other forums on this topic and have tried different things without success. However, none of these options have worked.
The decompiled 'main.class' file is currently stored within the 'JRE system library' folder. Is this the wrong place to store it?
I would be so grateful for a helping hand!
I have provided an image of the decompiled code:
And the run configuration tab, where I specify how I want the code to run.
I have now created a plugin using 'Maven', which allows me to edit the code given that to use Bukkit as an API requires the tool 'Maven'.
https://bukkit.fandom.com/wiki/Plugin_Tutorial_(Eclipse)#Example_Files_and_Templates
Thanks so much for the help :)

Java, Eclipse, Could not find or load main class

I have a java project: simple telegram bot. It worked while I didn't tried deploy it on Heroku. I did a backup for stable version, but Eclipse keeps showing me this ERROR. IDK what's wrong with it, I tried a lot of stuff, like what's the problem? I have src folder, classes also, I have main... Maybe problem in .gitignore file?
But I deleted from it *.class, it's still not working. Help
It's not a duplicate, because I'm workin in Eclipse and it worked earlier. Added my folder properties. I need short answer, not just a wiki of possible problems because it doesnt help.
Folder props
Screenshot error and folders
Your project configuration is very bizarre; you have class files in a directory named 'SRC'. It's hard to tell what you did to manage to create a project def this bizarre. Some ideas:
You've compiled this on the command line. You should probably look into build systems like maven or gradle to build your code. Even if not, use the -d switch on javac to ensure the class files end up in a directory named 'bin' or 'build'.
You've turned off eclipse's autocompile function. Turn it back on.
Right click your Point.java file (not Point.class) and pick 'run...' from the context menu. Assuming autocompile is on this will work.
Generally, use packages. Stuff in the default package is unrunnable in various scenarios. From what I can tell you're not in one of those 'default package is not runnable' scenarios but perhaps I'm missing something.

Loading Images from Jar file only works on Development Machine?

I am having trouble loading Images contained within a JAR file. I have read a number of other posts related to this. But cannot find the answer. Now here is where it gets complicated.. This all works fine if I am using a Runnable JAR file exported from Eclipse and Run it using the standard JRE. However This JAR file is actually a plugin for a piece of software called pro/ENGINEER which has it's own JRE that is used to run the JAR file. The strange thing is.. that this works fine on the development machine, that has eclipse installed and so on, but doesn't work on any of the client machines?! But I can't see what is different.
I am trying to load the image using:
ImageIcon icon = new ImageIcon(this.getClass().getResource("resources/Header.png");
This method is called from my 'Start' class, and the Jar is structured as follows:
load
Start.java
load.resources
Header.png
If I Open the Jar file with WinRar you can see the Image definitely exists in the jar, in this position.
I know this may be a very specific question but if anyone can be of any help that would be great.
I did not have the particular problem you're having now, however I ran into the problem of not finding a resource in a JAR file several times in the past.
The solution was to not get the resource from the class, but from the class loader as demonstrated below:
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource("resources/Header.png"));
Mind the .getClassLoader() call after the getClass().
Hope this helps.

Eclipse cannot create runnable jar - No resources selected

When I try to export my project as a runnable jar Eclise gives me the following error:
No resources selected.
The project runs fine within Eclipse, I already tried to clean the project before exporting, but that didnĀ“t work.
Any ideas on how to fix this.
The problem is most likely with your run configuration.
Go to your Class that contains the main(String[] args) method you wish to automatically run
when the jar is called from the command line
Right-click->Run As...->Application
Make sure it runs to your satisfaction
Now go through the export process as before, selecting the newly created launch configuration.
The last step is key.
I found another solution.
I found that there's a bug in JDK 9.
I solved it by downgrade to JDK 8(1.8.1)
I fixed the problem, I exported the project as an archive file. Then I opened up a new workspace and imported the the archived project. After that I tried to export the project as a runnable jar, after two tries the jar runs as is it should.
I still don't know what the exact cause was but I hope someone how has run into the same problem finds this helpful.
Doing a (project->clean) and creating a new run configuration (with correct project and class name with main() method, selected) solved the problem for me.You just have to select the new run configuration while exporting runnable jar :)
Try this, this is the way to create a jar or runnable jar in eclipse, all your external libraries in the project will be included
File -> Export-> Java ->Runnbale JAR file
Launch configuration : your Class containing the public static void main(String[] args)
Export destination : Target place
Library Handling:
Package required libraries into generated JAR
FINISH
When you click this option in Eclipse you should get a popup that lets you choose what files to export, you need to select the src folder.
I was having this same problem, and I was able to resolve it. Maybe you should cross verify the main class, that you selected in this project. It is possible that some other project's main class may have the same name as the main class you selected. Make sure the main class you selected is the one from the project that you selected.
The problem is your package. You probably renamed it, and in the export option, the program was confused, and it was using the first name of your package.
This is basically happen when you change package name under which you made your program. This will create confusion regarding package, as per program export, main method still written in old package.
To resolve it, Just copy it and paste it in same package with different name and then try to export it. Check for log too.
I was in the same situation. For me deleting existing eclipse installation(not not the project workspace) and opening the existing eclipse workspace with new installation of eclipse was the only thing that worked.
Just copy the project as a new one. Configure the build path by creating new Run/Debug Settings.this would resolve the problem.
I continued to get the "no resources selected" error. I then did the procedure I am quoting and it gave me a MANIFEST file. Then I was able to create an executable JAR.
Quoting: "I exported the project as an archive file. Then I opened up a new workspace and imported the the archived project. After that I tried to export the project as a runnable jar, after two tries the jar runs as is it should."
I also faced the same issue, in my case, I just closed the project and reopened it, now it's wokring
Check that your project is correct at the time when you select the main class.
If the class non exist, it's the standard error of Eclipse.

Eclipse create java executable with external libraries

I've seen this topic in this forum but it I need a more basic explanation on how to do this.
I've done a program in Java with some external libraries (LWJGL and Slick).
So this is what I've done and my program won't start anyway, tell me where I've done wrong.
I have Eclipse 3.7.1
My project is opened in Eclipse and runs well in Eclipse
I click File -> Export
I select Java -> Runnable JAR file
Here I don't know what to choose in Launch configuration, when I click the dropdown I get the option to choose my main class so I do that.
I select an export destination
I select the option "Package required libraries into generated JAR" under Library Handling
I don't know what ANT script is so I don't use that
I click Finish
I copy my images-folder to the same location as the generate JAR-file
I try to start the JAR-file, something loads in the background but nothing happens, no window shows up, nothing.
I check the Task manager in windows and sees that a javaw.exe is running
What did I miss?
My program uses images for graphics like this:
image = new Image("images/filname.png");
I wonder if I need to change the paths before exporting or the method to load these?
Thanks!
If you want to get things from inside a jar file you need to have them in your classpath and access them as resources.
The constructor you use, refers to a physical file which cannot peek inside a jar-file.
(EDIT) Also note that you have no guarantee where the current working directory is. Hence any relative references may break, as you see.
Read this to learn how to use JarSplice to export your Eclipse project to a runnable jar-file. Regarding images, you can put them in the same directory as a class file (in the jar) and then writeSampleClass.class.getResourceAsStream("image.png")to retrieve an InputStream of the image. Then you can load it however you like.
I had the same problem and I was able to fix it. All I did was copy the data folder (which contains my resources) into the *.jar file. You can do this for example with WinRAR.

Categories

Resources