How would I compile a Java application including multimedia? - java

I wish to compile my java project in eclipse but I am at a loss and I can't figure out how to include my pictures and my database in the project or how to compile them all into one .jar file. Any help will be appreciated.

To add to what Bruno said. If you're using eclipse to build/package your project then:
add resources subfolder to project root folder
right click project -> properties -> java build path and in source tab add the folder you just created.

You can load data from InputStreams obtained from the classloader (using data on the classpath, possibly in a jar) instead of a FileInputStream (assuming that's what you do) as described here: http://download.oracle.com/javase/1.5.0/docs/guide/lang/resources.html
For the database, it will depend on whether the database engine you're using can load from the classpath. This method doesn't allow you to write, and some database engines may require write access for locking (depending on what you do with the DB).

Any file/directory you copy under the /src of your eclipse project will be included in the jar file created by eclipse and will be accessible from the java programs of that project as classpath resources.

Related

How to edit .java files imported in Eclipse from a .jar file

I have a question, perhaps it was already answered, but i didn't manage to find it and I appologize if the solution already exists (let me know if it is before deleting my thread).
Problem is:
I have created a program on another PC and exported it from eclipse as a .jar file. It works on my main PC when I double click on it but when I import it in Eclipse I can't find the .java file. So i can't edit it.
What I have done so far:
In eclipse I have created a new empty project
I have right clicked,import, archive file, selected the .class files that eclipse sees, but when I am in the Project Explorer in Eclipse I can't find the .java file where the main is. I mean I can click run as a program and it works, but there is no .java file, only .class files. What am I doing wrong?
That cranes.class should be cranes.java. At least on my other PC it is.
Program works fine, but I can't edit it on my main PC. What am I doing wrong?
Thanks and best regards
You need to select the Export Java source files and resources option while creating the jar file and then your Java files will be available on importing the project from the jar file.
This is similar to how you use other libraries. You depend on the Jar file which contains class bytecode (compiled) of java code. You can't edit any of such files directly in the project you are using it. Thought you can always extends functionalities in your current project using simple inheritance concepts.
If you think such functionalities are trivial you should prefer to change in the original project rebuild the jar and use the newer version of jar.
However if you feel similar things for 3rd party library you can
always make changes after taking fork from those library source
code (if open source) and build and use your own version or go
ahead and raise pull request if you are confident about your
changes.
Mostly when you build a jar file, all you have in it are .class files; these are the result of compiling .java files, and so are not editable with text editors.
You CAN create a jar file that contains .java (also known as source) files, and even a jar file that contains both .java and .class files, but if you ask eclipse to create a jar file, by default it is just going to put .class files and files from resource folders in it, not .java files.
Assuming from the question, the jar is a library created by OP, by compiling java files into class files and packing/exporting them. Although the class files can't be edited in any IDE, they can be de-compiled into Java files by using third-party applications.
I personally use IntelliJ for this de-compiling source files authored by me
Note: Although this gives OP the desired functionality, it may lead to violations if the classes are Copyrighted.
As IntelliJ states, they neither encourage nor discourage de-compiling class files and the decision is purely to the user's discretion.
EDIT: It is always recommended to use the original source files for editing. Try to host them on git so that it may be retrieved anytime required
It may be simpler to not use eclipse but jar/zip/tar your project directory on the one computer and simply extract it onto the other, then open that folder as a new project in Eclipse.
Best is the suggestion from #SanjayBharathi to use git and clone the repo on your other machine.

class not found exception in eclipse even though the user library containg jar file is in java build path

I have created a dynamic web project. A user library with a jar file is also created.
Then the user library has been added to the build path via
project properties--->java build path--->In libraries tab required user library has added and jar file is specified under it.
But exporting the project into the JBoss deployment directory is raising class not found exception over the class in added jar file.
How can I properly add a user library to my project's build path?
I'm not clearly understand your problem but there is nothing to discuss ;)
Because the Java EE Specifications and the application server are dictating how to place something in your app's classpath and that's it.
Here is the documentation:
https://docs.jboss.org/author/display/AS71/Class+Loading+in+AS7
If your problem is not regarding the result on the appserver, only something about comfor or project strcture you want in your IDE. Then use your build system (Maven) to do the job. Like bilding a jar and coping it to desired location etc.

How to acces & modify resources in Eclipse during runtime

I have some resources that I'm working on in an Eclipse application. I need those resources to be available for access and modification by every other plugin.
I tried to place the resources under a source folder, but that causes Eclipse to copy the resources under bin, i.e. in a jar file. Therefore I can't modify them in runtime.
My question is, where and how do I place my resources so that everyone is able to see them and the resources are available for modification?
You can use the Workspace Mechanic plugin . It allows you to export , record and import preferences based on mechanic task files. You can have LAST MOD and RECONCILE tasks. For more information : http://code.google.com/a/eclipselabs.org/p/workspacemechanic/
Hope this helps.
Here's my solution:
I created a fragment project which only contains my resources and nothing else. To prevent Eclipse from copying my resources to a .jar file on runtime, I added the following to the MANIFEST.MF of the fragment project:
Eclipse-BundleShape: dir
Bundle-ClassPath: data/,
.
("data" being the folder where my resources reside.)
Thusly my resources remain as they are, and I can modify them however I want during runtime.

Add resources into a jar upon building

I want to add DLL's, images, textfiles etc to my project as resources so when I export it, the jar contains the resources so they can be used. I am using eclipse.
Problem is I have no idea how to add it. I've tried adding DLLs/pics to the src folder in the project, but when I export the jar, it is not located there
I've looked at How to make a JAR file that includes DLL files? but it only explains how to extract it, not how to add it to the project and build.
EDIT: I am using an applet to open the jar by the way, sorry for missing it!
Cheers
How are you opening the file in java?
Class.getResourceAsStream(name)?
If you are packaging the code in a jar, then you need to use that command. (as opposed to new File(name), which will get the file in the same directory as your jar)
If the file is not physically in your jar, you can check by changing .jar to .zip and extracting it, then check out this doc http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javac.html
Usually in an eclipse project, the src folder is the wrong place to put non-sourcecode-content.
You should try moving to maven as your build system, as it is highly customizable and provides you with folders inside your project for exactly that purpose. (src/main/resources)

How to add class-files to new java project

I'm tasked with converting an existing Java/C++ mixed web-application to pure Java, but I'm hampered by some missing Java sources (.java files) for which only the class-files are available. Fortunately I don't need to change anything in that code, just need to continue calling the methods.
I created a new Java Web Application project (using Netbeans); recreated the applet by copying it's sources in and got it working in a skeletal fashion, with the calls to classes & methods not in the sources commented out, but I am now stuck on how to add the class-files (of the missing sources) to this project.
(I'm new to Java, obviously) Any pointers on how I should proceed will be most welcome.
Package the .class files in a jar.
$ jar cvf my-library.jar the/package/*.class
Add this jar to the CLASSPATH of your project/application. In Netbeans:
go to the project view on your left
then right click on the library option,
then click add JAR/Folder option.
To add some source/code to your project, classic java project or webapp java project, you have to declare the path to the needed class/jar in the classpath variable.
If you are running your dynamic web project via eclipse, just add the path to the classpath tab in the "run configurations" of your server.
To learn more about classpath, see wikipedia.
The usual approach is to collect all these class files in a JAR file (use the jar tool) and put them on the classpath.
Different from C/C++, for Java you don't need the source code to these other pieces in order to compile the applet. There is enough information in the class file for the Java compiler to do what needs to be done.
So you can uncomment the calls to this code and follow the instructions in the other posts to put the class files on your project classpath.
In the Project window, right click on your project and select Properties. Go to the Libraries category. Then click Add JAR/Folder and select the location of your .class files.

Categories

Resources