Script file can't create a java Jar file - java

I have the following script in a bat file.
path=C:\Program Files\Java\jdk1.8.0_181\bin
prompt $$
cls
javac GUI.java
echo Main-Class: GUI>manifest.mf
jar cmf manifest.mf GUI.jar GUI.class
java -jar GUI.jar
pause
The Java file is compiled and the manifest file is created. However, when it comes to creating the Jar file, the command prompt gets stuck (hangs). It behaves as if it were on a loop.
! [Command Prompt Output]: https://i.stack.imgur.com/uO7gc.jpg
Any help would be appreciated.

According to this (under jar command options > m) you should switch the manifest and jar file name, so jar cmf GUI.jar manifest.mf GUI.class

The script was named 'jar' which caused problems. I renamed the file and it worked.

Related

executing java -jar via classpath vs in the jar file dir

After having used NetBeans to create a Java program call it Addition and then having successfully cleaned and built an Executable Jar File in a folder c:\Users\Ben\Doc\NetBeansProjects\Addition\dist
WHY is it that when executing, from command prompt,
c:\Users\Ben Java -Jar -cp "c:\Users\Ben\Doc\NetBeansProjects\Addition\dist" Addition.jar
it does NOT work (i get 'unable to access jarfile Addition.jar)
BUT if i use cd to change my current dir to c:\Users\Ben\Doc\NetBeansProjects\Addition\dist and THEN run 'java -jar Addition.jar' from there, the Addition program runs fine
The -classpath argument is ignored when you use the -jar option. See the documentation.
because java doesn't look in classpath to launch jar file for this command it needs file as input
so if you set the directory where your jar file is placed and try to execute java -jar command and expect it to pick up jar from that directory because it is in classpath it is not valid
you can give full path to jar like from any directory
java -jar c:\Users\Ben\Doc\NetBeansProjects\Addition\dist\Addition.jar

Run Jar File Does not work. Can't find Main

I use netbeans to create my java projects for school. I don't like how netbeans uses an internal console instead of the usual black CMD/Windows/Terminal console so I decided to compile my project via Command-Line using a batch file. My batch file refuses to run the jar though. It says that main class is not found. I cannot figure out why :S
Can someone help me or tell me how to fix it?
All the information needed to help me I believe is below:
#echo off
set ProjectName=WildWidgetsWarehouse.jar
set ProjectPath=C:/Users/Brandon/Documents/NetBeansProjects/
set path=C:/Program Files/Java/jdk1.7.0_11/bin
cd /d %~dp0
ECHO.
dir %ProjectPath%/*.java
ECHO.
ECHO.
for %%* in (.) do set FolderName=%%~n*
for %%* in (..) do set ParentDirectory=%%~dpnx*
javac -d ../Classes *.java -cp ../Classes;std.jar
cd %ParentDirectory%\Classes
jar cvf %UserProfile%\Desktop\%ProjectName% %FolderName% .*
java -cp . %UserProfile%\Desktop\%ProjectName%
PAUSE
When ran, it prints:
Invalid switch - "Users".
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
.* : no such file or directory
Prints all the file it added.. It adds all my classes.. See picture below
Error: Could not find or load main class C:\Users\Brandon\Desktop\WildWidgetsWar
ehouse.jar
Press any key to continue . . .
My Jar file looks like:
to run a jar file, you need -jar option
java -jar xxx.jar
please man java, read -jar option part.
also you may want to add main-class information into the manifest of your jar file.
man jar, and check out the e option.
the MANIFEST.MF of your jar should look like (just example)
Manifest-Version: 1.0
Created-By: 1.6.0 (or maven, ant, blahblah)
Main-Class: com.yourpackage.MainClass

Error unable to access jarfile C:\Jar

My jar file P2.jar (which has a main method and manifest file) is located in C:\Jar Folder. I tried to run it from cmd like this:
C:SomeRandomFolder> java -jar C:\Jar Folder\P2.jar
I get the error:
unable to access jarfile C:\Jar
It works when I run it from the folder that the jar is in like this:
C:\Jar Folder> java -jar P2.jar
Why can't I use the 1st command? Why do I have to go to the directory of my jar file?
That's because of the space you have in the path name. On Windows, use quotes:
java -jar "C:\Jar Folder\P2.jar"
If you are in a terminal and the folder you are in is deleted out from underneath you and replaced with the same content, the jar file will be be visible with ls, but will not be available since your current directory was deleted and replaced with one that looks just like it.
I got that error too:
el#apollo:/myproject/build/jar$ java -jar CalculateStats.jar
Unable to access jarfile CalculateStats.jar
To fix it. cd up a directory and cd back in, like this:
el#apollo:/myproject/build/jar$ cd ..
el#apollo:/myproject/build$ cd jar/
el#apollo:/myproject/build/jar$ java -jar CalculateStats.jar
Program completed successfully

How to run java class from .jar archive?

Need to run particular java class from jar package through unix console. Is it possible? Thanks
It's not possible to call a specific method of a class from a terminal.
You can only call the main method with java -cp JarFile.jar package.ClassName
Or, if your jar contains a manifest file, then you can do: java -jar pJarFile.jar.
Use
java -cp myJar.jar myClass
The -cp option is used when setting up the classpath manually ie give full path of location of jar file and then run command
Eg.
C:> java -cp C:\java\MyClasses\myJar.jar myClass
For running a jar file you can have either of following approach. Both of them require that you know the fully qualified name of the class where main(String[] arg) method is written.
Say your class containing main method is com.myclass.MainClass
You can run the jar directly. Keep the jar file at the location from where you are running this command
java -cp yourjarfile.jar com.myclass.MainClass
Create a manifest manifest.mf file with following content
Main-class: com.myclass.MainClass
now create the jar file as follows
jar -cmf manifest.mf yourjarfile.jar <your class file location>
Run this jar with the following command
java -jar yourjarfile.jar
Yes, use the -jar parameter:
java -jar MyProgram.jar
you can extract the specific class file by doing:
jar xf MyProgram.jar theclass.class
it will end up in your current directory
then just run the classfile
java theclass

Packing files as jar file

I am trying to create a jar file off of a class and xml file I have in a directory called SOACustomFunction.
OS: Windows 7 Enterprise - 64 Bit
Java: jdk1.6.0_21
I tried using the command below and I keep getting the error:
Error: "Unable to access jarfile SOACustomFunction.jar"
C:\SOACustomFunction>"C:\Program Files\Java\jdk1.6.0_21\bin\java.exe" -jar -cvf
SOACustomFunction.jar *.*
Any ideas what I am doing wrong?
Thanks,
S
The command you show is what you would use to EXECUTE the jar file. Use jar.exe instead of java.exe -jar
Typically you would have
jar cvf yourJarFile.jar yourClass.class yourSecondClass.class
java -jar is for running a class from a jar file.
Assuming your classes and xml reside inside your SOACustomFunction directory, the command would be
cd SOACustomFunction
jar cvf myjar.jar yourclass.class yourxml.xml
But do maintain a package structure for your classes and other resources instead of having them all in the jar's root directory.

Categories