Exporting Java game with Eclipse doesn't work [duplicate] - java

This question already has answers here:
Add image to JAR Java
(7 answers)
Closed 8 years ago.
I am trying to export my Java project using Eclipse's function but when I run it, it only shows the background, no external files. I used customized Checkboxes and Button, but they don't show up. Running it from Eclipse works perfect. This is the hierarchy for my Game:
.settings(folder)
bin(folder)
src(folder)
.classpath
.project
some .png files
This is how it looks in eclipse. I don't know what is with the red exclamation mark on Game.
These are the steps I took in creating the runnable JAR file:
I added my pictures to the Build Path (Skipping this step has the same effect)
I clicked Export - Runnable JAR file:
Then:
When I clicked finish, I had no warnings:

Why did you put all those images as referenced libraries? You should just put them in the src folder.
Any way, have you specified the main class when exporting the executable jar file?
Can you explain what happens when you try to execute the generated jar file?
These warnings may not be related to problem. But if you show them, it may be easy to check.

If I didn't get it wrong, Eclipse will only pack things in bin to the jar file. So you got two options:
Leave things as it is, but when deploying, put the images in the same folder with the jar file.
Put your images in src (so it would be copied to bin and hence packed) and change your way to access your image: get the URL with getClass().getResource() and read from the URL with something like Toolkit.getDefaultToolkit().getImage().

Related

What do I put as a file's path if I want to use it in a program that's on several computers? [duplicate]

This question already has answers here:
How to includes all images in jar file using eclipse
(4 answers)
Closed 6 years ago.
I have an image file which I want shown as a JLabel in a JFrame for a program that will be on several computers running the same code. The image would not be on the computers already but would somehow be stored as a program file. The computers would all be windows. To insert the image a file path has to be given, but I'm not sure what this will be considering the computers are all different.
I have done this before on a different language by having the program find the program's directory and doing some string manipulation, but I have not been able to do this yet on java and would like to know if there is a better way.
Here is the statement that the path has to go into:
lblTitleBG.setIcon(new ImageIcon(file path goes here));
lblTitleBG is a JLabel.
First you create a source folder inside your project using eclipse or your preferred IDE. That will create a physical folder in your project folder that you can navigate in and move your files.
Let's say you have Project1 where you create Files_Folder. Now you navigate to the Project1 folder with window explorer and paste the files you need inside the Files_Folder and in the bin folder too. After this, just go back to eclipse and refresh/clean project. Your new moved files will appear in the Files_Folder in eclipse explorer too.
Just use lblTitleBG.setIcon(new ImageIcon("Files_Folder/image.png")); after following all the steps and you will have portable code.
Try to pay attention to steps and everything will work good. And finally, when you export your jar, don't forget to keep the Files_Folder in the same location to the jar, in order to find and use the needed files.

How to hide source code of my jar file [duplicate]

This question already has answers here:
Is it really impossible to protect Android apps from reverse engineering?
(10 answers)
Closed 7 years ago.
I am Android developer.
I have created a .jar file from Java project with below steps
Right Click on project and select export option.
Selected jar file from Java folder
Clicked on finish. Then mysdk.jar is created on desktop.
When I pasted this jar in lib folder of Android Project. When I selected(ctrl + left click from mouse) any method presented in jar from AndroidProject/src/demo.java , It is showing source code of .jar file.
My requirement is to give this jar to Third Party Company who should not read my code.
So, I want to hide my source code of jar. but how ? Please some one help me.
I am using Android Studio.
Thank you in advance.
Extract the created jar file and check if only contain the class files or source file. Actually it shouldn't contain source files in jar files. If it contains , make use of jar command in shell/command line to create jar file and deliver it. Also check the new project is linked the previous project because both exists in the eclipse.
jar -cvf <jar file name> files to be archived

Why doesn't Eclipse export images? [duplicate]

This question already has answers here:
Eclipse exported Runnable JAR not showing images
(8 answers)
Closed 7 years ago.
I am writing a simple game in Eclipse (Java), it requires some images. It works fine in Eclicpse, but when I export it (as a runnable jar) there are no images (just empty places instead of them).
I have a source folder called "images" in the project, and I adress the images this way: "images/name.png". THe images are exported right into the jar.
I have tried placing images folder in an resource folder, still didn't work. I've also placed the images in different places in the jar, didn't work.
How do I make Eclipse export the project correctly?
(I'm just a begginer, so I don't know which information should I post)
If you want to include and access resources as images or configuration files from a runnable jar, you have to place them in your source folder to include it. Alternatively you could provide the full path to the resources, which of course makes it difficult to port to other systems...
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());
http://www.mkyong.com/java/java-read-a-file-from-resources-folder/

How to make executable file independent of Eclipse [duplicate]

This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed 8 years ago.
I have one library, one txt file for import some things with it and one output file to show the result. Only have one java file without any interface. With eclipse all is running well - I am writing into the input file and then it shows the result into the output file. Now I want to make this all independent program (without need to run with eclipse).
How to make it?
An easier way is to make a runnable JAR file.
Go into eclipse choose file > export > runnable JAR.
It will then ask you to choose a class for a launch configuration. Choose the class that has the main method for the desired program. After that you will have a file that will run (like a .exe but just a different format).
If can't get it to work with your input and output files, thats because eclipse has this weird thing that sometimes occurs regarding the location of external sources when exporting to JAR. To fix it, just put the files instead of the src (source) folder, one folder up in the directory. For example, if this is your current directory: C:\workspace\myprogram\src put it in C:\workspace\myprogram
Hope this helps.
The only thing that eclipse is is a software that facilitates progtammi g. But what find the programs is the jvm-java virtual machine. To get to the point eclipse does have a option to convert it to a .jar file which the extension a java program uses to be "independent" from eclipse and open up by double clicking. The way you do this is you right click on the class you want to make independent and I think there should an option that says export to .jar and that should do the trick! Hope this was helpful

Eclipse error with archived java projects "editor does not contain main type" [duplicate]

This question already has answers here:
Error: Selection does not contain a main type
(24 answers)
Closed 8 years ago.
I am receiving the errors: Editor does not contain main type, or, Selection does not contain main type when I import an archived Java project into Eclipse and try to run it. I have gotten this error on several different operating systems.
Archive Protocol
Export project
General->Archive File
All are selected: project folder, .classpath and .project
[x] save in zip format
[x] create directory structure for files
[x] compress contents of file
A Work Around That May Explain It... but how?!
I notice that when I import an archived project into an existing project (a blank Java project), The folder hierarchy is something like:
Project
src
bin
imported_project
bin
src
actual_code.java
Now, when I try to run actual_code.java, I get the above error. I discovered the following work around: if I drag actual_code.java and drop it into Project->src and then run it, it works.
This is a nice work around. However, I would prefer to be able to run the imported project without moving things around. Any suggestions on how to do this? It seems like it should be a trivial fix--it simply seems like the project isn't importing to where it ought to.
And yes...
I have tried every method I have come across to remedy this. That includes quite a few from this site and others: syntax, libraries, source path, restart eclipse, rearchive, different operating systems, different machines... etc.
The .java files are in the src folder before I archive them, and my build path seems correct.
You could import the archive as a Project instead of as files into a newly created blank project. Use Import > Existing Projects into Workspace. Then, use Select archive file.
You might want to put the src folder of your imported_project in classpath.
Right click on your project -> Properties -> Java Build Path -> Source -> Add Folder
I also faced the same error ajnd after reading above article i just drag/drop my class-file to src and new class-file generated in src is working fine .
I had the same problem after I New/Java Project then pulling src, docs, lib, examples, ... folders. I could not import it as an Eclipse project since the code is not an Eclipse project. The main is right there in the example code, but the IDE would find it.
The solution for me is deleting the project from the IDE, then recreating it with the Eclipse IDE. When recreating, the IDE cached all the sub-folders for me. Also, make sure you add the code onto the Java Build Path as Source using the 'Configure Build Path...' of the project.
Restarting IDE, closing then reopening the file didn't help me.

Categories