Jar file execution through use of BAT file - java

I have created a simple class with only inherent Java dependencies (java.io, etc).
I have set up my jar file and the bat file in the same folder. The BAT simply reads:
java -jar "MyApp.jar"
pause
I have been able to run it from several different locations on my computer. But when I sent it to a coworker as a zip file, he was unable to run it by double clicking the BAT file.
The command window came back with an error
could not find the main class: MyApp.MyApp. Program will exit.
I've poked around this site but most similar errors involve use on the same computer.
Yes the other computer has Java installed 6.29
Any help much appreciated.

Two options that I can think off the top of my head:
1) He might not have extracted them both to the same directory (or) after extraction, he might have moved around the JAR file to another location.
2) His classpath does not include the current directory. Your classpath has a '.' (indicating the current directory) while his doesn't. If that is the case, you can probably modify your command to include the '-cp' switch.

In order to run a jar that way, you need a META-INF folder inside it with a manifest file inside that. The manifest file needs a main-class line that points at your class with a main(). Your IDE probably added that, but maybe in the process of extracting things he unzipped the jar file too, or something "interesting" like that.
http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Related

My .exe file from launch4j does not work like my java file. Am I missing something?

As someone new to java and bundling programs with code, I was able to successfully get the proper output running a simple new HelloWorld java file. So I believe there are no issues with the java file in terms of compiling it to a class file or running it afterwards, and that I have all the files needed in the java kit to create an executable file. However, I am not sure if I am using launch4j properly to get the .exe, either with setting up the .jar or through the process from making a proper .xml file.
The code below shows what I get when I try to put everything into the .jar file, but I guess the output is an error because a new .jar file isn't produced unless I take out the "m" from the "cvfm" in the jar command. The code does show something about the manifest being added regardless when doing so, yet I still do not get a proper application. After getting the correct output without launch4j, I stopped recreating the .java and .class files and just focused on the .xml when recreating the .jar file achieved no difference. I have tried in launch4j leaving the environment variables blank in the JRE tab or just included the same path of the system variables that the java file worked with correctly in the command prompt, and I have also switched the check of GUI to console in the header tab. Research has also told me to look up a manifest.mf file, to which I don't think I have that precise file in the JDK, but may have found something similar in the kit (at least when looking in typical areas like the bin folder).
C:\JavaTest>jar cvfm HelloWorld.jar HelloWorld.class
java.io.IOException: invalid header field
at java.util.jar.Attributes.read(Attributes.java:406)
at java.util.jar.Manifest.read(Manifest.java:234)
at java.util.jar.Manifest.<init>(Manifest.java:81)
at java.util.jar.Manifest.<init>(Manifest.java:73)
at sun.tools.jar.Main.run(Main.java:176)
at sun.tools.jar.Main.main(Main.java:1288)
C:\JavaTest>jar cvf HelloWorld.jar HelloWorld.class
added manifest
adding: HelloWorld.class(in = 426) (out= 289)(deflated 32%)
I always get a warning about signing when testing the wrapper, but I don't think that has been an issue like an actual error. Due to the nature of the numerous combinations, it is hard to keep track of what caused the differences in issues, but it seems that now leaving the JRE tab blank except for having a min JRE version yields the error "no main manifest attribute" right from the wrapper test in launch4j. Having the very end of the system variable path included in the environment variable field does the same thing. Before trying to recreate the .jar, switching the header to console would create a .exe without errors, but either opening the application would either do nothing or put the same "no main manifest attribute" output in the command prompt. Now, I can't even use launch4j to test wrappers that have the header on console even when building them produces no errors (yet the same error happens when opening the .exe). I am just trying to get the .exe produced from launch4j to provide the same output in the command prompt that I get when typing "java HelloWorld" there.
If I am indeed creating the .jar properly and working with launch4j properly, did I just miss the unlisted step of needing some sort of manifest file to work with launch4j? If so, how would I make sure I got it properly? Would it be seen in a bin folder or completely separate from the JDK? Would I need to move it to my JavaTest folder where the java/class/jar/exe files are? Any help is truly appreciated.
The 'm' in jar cvfm stands for manifest, and implies that you will be providing a file as argument which is the manifest. The f stands for: You will specify the file name.
So, HelloWorld.jar is the argument to the f, and HelloWorld.class is the argument to the m. Your class file, obviously, isn't a valid manifest file, hence why the error occurs.
Generally, use a build tool to make jars, such as maven or gradle. You need a manifest in order to have a Main-Class attribute, and you need a Main-Class attribute to create a runnable jar, and you need a runnable jar to launch4j-ify it.
Make a file named MANIFEST.MF. Create it with a plain text editor. It should contain:
Main-Class: com.foo.thisIsAPackage.YourMainClass
and nothing else.
Then:
jar cvfm HelloWorld.jar MANIFEST.MF YourMainClass.class
note that I'm pretty sure you MUST have a package or this is not going to work.

Building file.jar, External Executable File missing

Assuming that I use NetBeans 7.3 , I created a project that, in a nutshell, receiving as input a set of parameters, it returns as output a print on screen. The project is made up of a number of directories. Each directory contains a class (in file.class form). One of these directory contains an executable in C. I wrote it as the kernel of the Java project.
I built file.jar and I added it as a library in a new project. When I tried to test it, an error message made ​​me realize that the C written program is not was automatically added to file.jar under construction.
One of my first attempt to solve this problem was to manually add the C-executable file. By using the JAR command from the terminal on my Mac, I was able to update the file.jar adding the executable in the right subfolder.
This solution is not served because, moving from project to file.jar, the relative path that leads to the execution of the C-program has changed. So I tried to change this path seeing it from the point of view of file.jar. Yet this attempt was futile.
I defer to those with more experience than me in the packaging and distribution of Java content.
As far as I know, an operating system cannot directly execute an executable that is inside a zip file (which is what a jar file actually is). It has to be first extracted.
So your program could first open its own jar file and extract the executable file into a file on disk, then run that file.
You can create an installer program, to install both the jar file and the executable file to a suitable location on the user's disk.

How to make jar files that draw from a source folder

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.

Setting the classpath for JAR files

I have recently just created Java project using Eclipse that requires 2 JAR files (phiget21.jar and the mysql.jar)
Everything works fine when running the programme in Eclipse, and I have noticed the the jar files are saved in a 'lib' folder.
I soon going to me moving the programme off my computer to be used on other machines, so I decided to create a batch file to compile all of the classes and then run.
However, I am having trouble with the locating of the jar files. In the batch file do I require a command something like: set classpath=.:..;mysql.jar:../phidget21.jar, before the compilation of the Java classes?
I have read that the dots (...) have something to do with directories but not entirely sure how to implement them.
My programme is currently saved in these locations:
Project/src/.java files (I have also put the .jar files in here as well as i thought this may make thing s easier)
Project/lib/ .jar files
Any help would be greatly appreciated!
while setting the classpath a single dot (.) means current directory. As you jar files are in current directory, you just need to go to your current directory using cd command in DOS prompt, then use
set classpath = .;filename.jar;another filename.jar
Here . represents current directory and semicolon separates each classpaths.
You can even set classpath of more than one jar files using wild card character * which can be read as all.
You need something like
java -classpath lib/foo.jar:. com.company.Program
you can also use wildcards since java 6. see here
so the above becomes
java -classpath lib/*:. com.company.Program

The dreaded java.lang.NoClassDefFoundError

I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line: <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>)
Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
Are you specifying the classpath to java on the command line?
$ java -cp lib/commons-lang-2.4.jar your.main.Class
The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.
If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:
java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).
Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with
java -jar yourProgram.jar
There are several variants depending on your needs.
Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See #BalusC's answer for good coverage of that.
Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.
You can usually go to your project bin directory and do:
java -cp . MyClass
But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.
make sure your jar commons-lang-2.4.jar in classpath and not redudance.
I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth

Categories

Resources