I made a small application with java in Eclipse using SWT for my GUI. I set up the project by installing SWT Designer, which is very similar to Swing Designer only for SWT. I created the project by going to File >> New >> Other >> Window Builder >> SWT Designer >> SWT/JFace Java Project. This set up the whole project for me so I could start using SWT. My program successfully runs when I use Eclipse to run it.
I want to be able to run it without Eclipse, though. I tried to export it as a .jar by going File >> Export >> Java >> JAR file. I selected my project, hit Next >> Next, then I set my main class and hit finish. No errors.
Opening the .jar gave me:
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Layout
It was pretty clear that SWT isn't where it should be, so I opened the .jar and found only my files, the META-INF, etc. No SWT anything. No jar, no org folder, nada.
In my Eclipse project, under the Project Explorer, it lists "Referenced Libraries", where the SWT stuff is set up. When looking under the properties of it, it lists a bunch of org.eclipse... stuff. I assume this is how Eclipse knows what to run, but it seems to not export it.
So my question is: How do I get the project to export with SWT packaged with it, in order to be able to run by the .jar alone? It would be nice if you could explain both the manual way (i.e. editing the META-INF and dragging and dropping the required SWT files) and also explain the Eclipse export way (so I don't have to do the manual way all the time), if there is a way to do it in Eclipse.
EDIT: If you see my answer below, I explained how I was able to get this working, however, it dumps ~16MB worth of code into my .jar, which is pretty big for an originally < 1MB project. If you have any idea on how to make it only add the used files, an answer is still appreciated. ;)
Does not sounds like you are using OSGI, my suggestion would be to go to your eclipse plugin directory, grab the swt jar copy it into your plugin and add it to the classpath by going to manifest editor, runtime tab and selecting the jar. If you are running an osgi app, make sure your .product file has the correct dependencies by going to .product editor dependencies tab and selected add required plugins. If you are running this in a different context comment and I will answer.
The way I successfully made this work, was by exporting it as a runnable JAR.
My steps to getting it working:
Run the main class. (I specifically use CTRL+F11, the default run on the class, letting Eclipse guess how it's supposed to be ran.)
Go to File >> Export >> Runnable JAR.
Select my Launch Configuration. (It's listed when I hit the arrow beside the run button on the top bar on Juno.)
Hit the finish key and wait!
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 want to use the Light Weight Java Gaming Library(LWJGL) to my Netbeans so I can use it in my Java application. The only videos that I can find show the zip file that they downloaded with separate src and doc folders inside of it. The zip file that I download has everything in one directory. I went to lwjgl.org/download and clicked on Stable and then Generate Bundle. What am I missing?
I had the same problem recently.
So, to begin you want to go to Tools in the context menu and select Libraries (as shown). Next you can add a new library and name it e.g. LWJGL-3.1.1 confirm with ok. You can find 3 tabs in the current window Classpath, Sources and Javadoc. There you add your jar files accordingly (in the downloaded .zip file you find .jar files with different names like lwjgl-{whatever}-sources.jar or lwjgl-{whatever}-javadocs.jar) make sure you put them in the right place. You have to repeat this process for all of the jar files you want. The javadoc files are not required but recommended. Make sure you also collect all the native .dll files and merge them in a folder called \natives. You find them in these jar files that are called like: lwjgl-{whatever}-{your-OS}.jar.
Once you have finished the setup for your library right click on your current project and choose Add Library.... In the window that pops up you scroll down until you find your library that you have just created and you are almost done now.
Last but not least go to the project settings. Select Run and make sure that you set the classpath in VM options to something like in the image: -Djava.library.path="C:\java_workspace\LWJGL Library 3.1\natives. Now this classpath tells netbeans where your native files are located. Your \natives folder that you should have created in the beginning is where this path should lead to. That's it. This is all you have to do for a setup without the use of maven, gradle, ...
You can test if it is working with the code provided by LWJGL HelloWorld example.
I hope this solved your problem.
Best regards.
I'm posting this query after doing lots of googling and trying out different options for last few days. Is there an easy way to attach source in Eclipse? is the closest answer I found to my question.
From above post, I tried all solutions but each time Eclipse is opening .class file instead of .java file.
Eclipse Details:
Kepler Service Release 2
Build id:20140224-0627
Please let me know if this is a bug with Eclipse or if I am missing something.
If you are using Maven, try this:
Double click on your project in eclipse -> Maven -> Enable Workspace Resolution
This is most likely because Eclipse actually use the class file for declaring the item you have requested the declaration for, and not the source file you think it should be using.
The typical reason for this is if you have a jar file containing previously compiled classes on your build path before (or instead) of the project containing the source you want. This is very rarely desirable as it can take quite a while to discover that your edits do not take effect.
Examine your build path carefully and ensure it is as you want it to be.
Maven is likely supplying a Classpath Container to your project, and generating entries for the Java Build Path based on the pom.xml contents (the JRE System Library is another example of this). Its entries will not have source attachment unless the container decides it will, which is why the Installed JREs preference page offers to let you set Source Attachments. If the pom.xml has something like that, see if you can use it. If not, see if you can configure the Maven container from its context menu. Otherwise, you may simply be stuck.
I had this same problem: when I pressed F3 or tried to open Declaration (after right click) for any element, it took me to the bytecode .class file.
My solution:
I went to the project in Project Explorer:
right click on project name->Properties
Then in Porject Properties window,
Java Build Path->"Order and Export" tab
There I selected the folder with the .java files (source code), which was below the folder having the .class files -in my case "JRE System Library [java-11-openjdk-amd64]", and pressed 'Top' button on the right frame. Then the folder with source files moved to the top.
Finally, pressed "Apply and Close" button.
That solved my problem forever and ever.
The folder
I installed a plug-in which allows me to download the sources for a maven project.
I'm not by my develop machine right now, but I think it was this:
http://marketplace.eclipse.org/content/m2e-dynamic-sources-lookup#.U8gjGWIaySM
With this, you can select a project an have maven download the sources and make then available when you navigate the code in Eclipse. All without "leaving" Eclipse.
Check into your project modules the Java Build Path, look inside the Source tab and verify if you have "Allow output folders for source folders" checkbox flagged; in this case you just need to uncheck it and you will be able to see again the right source from java files.
This is related to the Call Hierarchy in Eclipse IDE.
If you are in a situation, where you're seeing duplicate methods inside the call hierarchy showing Members calling 'myMethod(...)', double-clicking on one of the listed methods may open the .class file instead of the .java file.
When this happens, the solution is to set the search scope in the call hierarchy to Project instead of Workspace.
After setting the search scope, you won't be seeing duplicate methods listed in the call hierarchy, and double-clicking on the listed methods will only open the .java files.
(Using Eclipse DevStyle Dark Theme)
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.
Alright, so for a homework assignment I had to make a simple application with java swing. I used the netbeans GUI builder to do it and it works fine. However, when I zip up the entire folder (Documents\NetBeansProjects\Lesson 7) my professor complains that he doesn't have all the files. What other files could there be? I'm zipping up the whole folder that includes build, nbproject, src, test, build.xml, and manifest.mf. Is there anything else I need to zip up, or is my professor doing something wrong?
Netbeans GUI Designer uses the:
Swing Application Framework (JSR-296)
Netbeans keeps those separate as libraries (see the Libraries Item in the Projects view of Netbeans). Netbeans Libraries can be viewed under:
Tools --> Libraries
In order to run your application, you will need to supply the 'appframework' and 'swing-worker' jar files so that they can be added to the classpath in order to run the application.