Ok so... here are my steps.
I have a folder on desktop. Lets called it DesktopFolder. Inside desktop folder, i have 2 folders. One called libs and one called src. Inside libs are two jar dependencies. Lets call them jar1 and jar2. Inside src, i have a java file. lets call it MyProgram.java
so I compile them in cmd with
javac -cp .;../libs/jar1;../libs/jar2; MyProgram.java
that compiles.
now I create a manifest.txt inside my src folder with the following:
Main-Class: MyProgram
Class-Path: ../libs/jar1 ../libs/jar2
<a new line>
then in my cmd, I navigate to my src directory and do:
jar -cvfm MyProgramJar.jar manifest.txt ./../libs MyProgram.class
this compiles a jar file called MyProgramJar
this is found inside my src (because i navigated my cmd directory to src)
When I run the executable, it works.
But when i move the MyProgramJar outside to desktop
it says it cannot find library. Why is that? How can I fix it?
The error itself is the JNI error.
hmmmm. thanks to #MadProgrammer (from the comment section). I learned that a jar file cannot access another jar file from within. The classpath you put into manifest is all relative classpath. Once you move the jar file outside, it wont be able to locate the file that your program depends on. There are ways around it like "fat" jars as mentioned by Madprogrammer that allows a jar to access another jar from within.
Another way is opening up dependencies jar and simply moving the files out of its own jar. That way, your code can reference those libraries directly.
A personal friend of mine used eclipse to build the jar. That seem to work even if you have a jar within a jar and you move the jar around. I'm assuming eclipse did something just like "fat" jar.
Related
I'm writing a MP3 player in Java.
If i will finish I want to pack all .class files in one .jar file. I don't want have player, which starts by console.
If i open this one .jar file i want see player window.
I know how to pack it. I must use jar.exe packer with params: cvfm Player.jar MANIFEST.MF ./config/*.class and create MANIEST.MF which content class that has main method.
But the problem is when I want use another Look and Feel, or use existing .jar file. I can put this existing .jar file into my player main dir and compile javac.exe with parameter -cp .;./JarFile.jar, but when I pack all compiled .class files and my existsing JTattooDebug.jar file into one Player.jar file i don't see new Look and Feel i just see default view.
If you are using eclipse.
Right-Click your project
Export -> Runnable Jar File
Then select the destination for your jar, and make sure you have "Extract required libraries into generated JAR" selected. That will give you a runnable jar, complete with auto generated manifest, with all of your needed jars inside.
Solved! I had to add line:
Class-Path: lib/JTattooDebug.jar
in my MANIFEST.MF file, now all works.
I've been wanting to make executable jar files with java lately. When executing my code with Eclipse it works perfectly. But when I use Eclipse to export the same code as a runnable jar, Most of my jars work except the ones that draw from separate source folders.
The jar will be made but when launched it will try and open and then just say to check to console for possible errors. I try and run the jar through the console with the command "java -jar test.jar". and It says it cannot access the jar. Any Ideas? Btw Im on a macbook pro osX. Thank you!!
picture of where my files are within eclipse
If you have a file you want to store in a jar and access from there, you don't really have a Java File any more. Look at Class.getResourceAsStream() and Class.getResource() - the first can give you an InputStream to the (used-to-be) file, the second returns a URL and can be used for things like images. Note that the file being accessed can be accessed relative to the package/folder location of the class or relative to a classpath root (by putting "/" at the front of the resource name ("/resource/funny.jpg")).
When you execute the jar from a command line, be aware that you have a thing called the "default directory"; it is a folder in which your commands execute by default. If your jar is not in the default directory, you have to specify a valid folder path to your jar to execute it.
I'm not very skilled in writing batch files and/ or java. I have a folder with several .class-Files and folders in it and I want to put them all into a executable .jar-File. I know that there is a tool called "jar - The Java Archive Tool", but that just won't work for me. My folder structure looks like this:
Folder "test"
File (a.class)
Folder "subdirectory"
File (b.class)
I want a executable .jar-File called file.jar. In this .jar should now be the file a.class and the folder subdirectory with the file b.class in it.
I don't get the .jar-Tool to run and the 7zip command line doesn't support .jars (I can't even add files to it). I want this to run from a .bat-File, so I just have to open the batch-file, it creates the .jar and puts the files in it and then closes itself again.
I hope you can help me and get what I mean.
If you have issues in executing jar command, then probably you would need to check if your path has been set correctly. You can verify if the path contains jdk location ( echo %path%) from command prompt. If not you need to update. Also you can verify using Javac -verbose command to see jdk is installed.
Once you have jdk path set, you can try below command to create jar
Jar -cf myapp.jar * --> includes all files including files from sub folders.
If you want to run from batch, you would need to mention path before jar command. Ideal place for path is to configure as environment property.
Create a text file (and call it something useful like manifest.txt). In it, put the line:
Main-Class: a
(which should be called A by convention) and include a hard return at the end.
Then create the jar with:
jar cvfm file.jar manifest.txt *.class
or
jar cvfm c:\somedir\file.jar c:\somedir\mainfest.txt *
You could just put that line in a batch file called createJar.bat and run it if you only want to create files called 'file.jar'
hth
My project runs fine from Eclipse.
But when I tried to make it into a jar file or executable file it doesn't work.
I used the option "Export-Runnable JAR file"
The following message appears just after the eclipse finished the exporting process
JAR export finished with wornings , see details.
the details were ..
Exported with compile warnings:Mario/src/Map.java
and the same for other classes like
Exported with compile warnings:Mario/src/Player.java
and so on.
So that I used the other option "Export - JAR file"
It works fine and nothing appears while exporting it from Eclipse.
But when I try to open the file it gives me
Couldn't find the main class:Frame.Program will exit
Somebody have any idea about what the problem is?
Your MANIFEST.MF file inside the META-INF dir should have a Main-Class attribute pointing to your main class.
The important thing for executable jar is Manifest. Make sure it exists and points to the correct class with main method
I wonder if this is possible with ant and java:
I have a Java project which looks like this:
./
build.xml
src/
com/
example/
{.java files here}
bin/
com/
example/
{compiled .class files here}
lib/
{3rd-party jar files here}
dist/
{jar file(s) here}
I know how to make a .jar file that contains the stuff in the bin/ directory, with the right manifest to make it run my main Java class.
What I would like to do, if possible, is to make a .jar file that:
Java can execute ("java -jar myproject.jar")
Someone else can unzip to create the project structure. (including all the subdirectories except for the "dist/" directory)
Is this possible? I can't seem to tell Java to use the bin/ subdirectory of the .jar file as the classpath, I may be screwing up the syntax somehow.
edit: Okay, it sounds like trying to make one object serve two (too many) purposes. I have abandoned this approach, instead creating a standard .jar file as the executable, and a .zip file with the source (the build.xml + the src and lib directories). That way there are 2 easy downloads, more than 1 file but not too bad.
No, UrlClassLoader always tries to find classes based in the root, and a jar URL will always fetch entries based in the root of the jar file.
You could create a jar file which has binaries from the root, but source files under src etc. That wouldn't be too bad, assuming you really do just have com as the only "root" package.
You could put your project on a public SVN server, and just include instructions on how to check out the source - you'd also benefit from other people's check-ins and improvements (hopefully not vandalism).