Results 1 to 15 of 15
- 03-08-2009, 12:44 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 58
- Rep Power
- 0
Creating a Jar File Using Command Prompt
Q1) Dear All, I have read the Java API and my classes are stored in a folder named CPE102. However, Windows XP refuse to acknowledge my command. Tell me what is wrong?
Refer to help.jpg
Q2) I have downloaded the JavaRunner file from the Java website. However, I am not sure where to pass the arguments. How am I siuppose to tell the program which file I wanna read?
In other words, where to pass the URL if I am accessing a local directory?
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.lang.reflect.InvocationTargetException;
/**
* Runs a jar application from any url. Usage is 'java JarRunner url [args..]'
* where url is the url of the jar file and args is optional arguments to
* be passed to the application's main method.
*/
public class JarRunner {
public static void main(String[] args) {
if (args.length < 1) {
usage();
}
URL url = null;
try {
url = new URL(args[0]);
} catch (MalformedURLException e) {
fatal("Invalid URL: " + args[0]);
}
// Create the class loader for the application jar file
JarClassLoader cl = new JarClassLoader(url);
// Get the application's main class name
String name = null;
try {
name = cl.getMainClassName();
} catch (IOException e) {
System.err.println("I/O error while loading JAR file:");
e.printStackTrace();
System.exit(1);
}
if (name == null) {
fatal("Specified jar file does not contain a 'Main-Class'" +
" manifest attribute");
}
// Get arguments for the application
String[] newArgs = new String[args.length - 1];
System.arraycopy(args, 1, newArgs, 0, newArgs.length);
// Invoke application's main class
try {
cl.invokeClass(name, newArgs);
} catch (ClassNotFoundException e) {
fatal("Class not found: " + name);
} catch (NoSuchMethodException e) {
fatal("Class does not define a 'main' method: " + name);
} catch (InvocationTargetException e) {
e.getTargetException().printStackTrace();
System.exit(1);
}
}
private static void fatal(String s) {
System.err.println(s);
System.exit(1);
}
private static void usage() {
fatal("Usage: java JarRunner url [args..]");
}
}
In short, I just want to create a jar file and then run it automatically using a click. HELPLast edited by hitmen; 03-08-2009 at 12:55 PM.
- 03-08-2009, 12:52 PM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
The problem is that windows doesn't know where the 'jar' executable is.
Go to your windows control panel, open up "system", click the advanced tab, and then the 'environment variables' button. Add the path to your jdk's bin directory to the 'PATH' variable, and then the command prompt will find the 'jar' command.
I don't know why the java installer doesn't set this up automatically, but it's very annoying.
- 03-08-2009, 05:27 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 03-09-2009, 03:36 AM #4
Member
- Join Date
- Oct 2008
- Posts
- 58
- Rep Power
- 0
It still doesnt work.Look at my screenshot below
BTW what is the diff btw a classpath and a path
and what is the difference btw a JDK and SDK? I thought they are the same
And is there a way to create a JAR file without using command prompt?
- 03-09-2009, 10:26 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
On what contest you are talking about? Did you set the path as toadaly explain. Can you show the path you have added there?
Both are development kits,
JDK - Java Development Kit
SDK - Software Development Kit
But JDK contain JRE which can/allow distributing with your Java application.
classpath define in the Path field of the system variables. Other than that there is no major difference, only for the simplicity use them.
It's possible with use of an IDE. Almost all Java IDEs can build the executable jar files.
- 03-10-2009, 06:25 AM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
- 03-10-2009, 07:43 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
The terms path and classpath both refer to a collection of locations, or to a variable holding the textual representation of such a collection. (For instance in Windows the names of directories separated by the ; symbol).
But they refer to very different collections with nothing in common.
The path is the list of places that your OS will use when you specify the name of an executable. In your case if you use the command "jar" the OS will look for various files - jar.exe, jar.bat etc - in all the directories of your path.
The classpath is the list of places the Java runtime will use when you specify a class name (or other resource). When you use a class name like mypackage.MyPanel then the runtime will look for the MyPanel class by searching the classpath for the mypackage package and, within it, the MyPanel class. The classpath can contain jar archives as well as directories. Yet another difference between the two is that a classpath variable is a waste of time as there are other, better, ways of specifying the classpath when the application is run.
(As mentioned, your screenshot does not show what the PATH currently is. But the problem is that the jar.exe executable is not to be found within the directories listed in the PATH variable. Check spelling and that you've quoted any string with spaces in it.)Last edited by pbrockway2; 03-10-2009 at 07:48 AM.
- 03-10-2009, 11:05 AM #8
Member
- Join Date
- Oct 2008
- Posts
- 63
- Rep Power
- 0
Open the command prompt and change to the directory that your java files are in. Then, type "path" followed by a space and then the path to the bin folder of your jdk e.g. path C:\Program Files\Java\jdk1.6.0_05\bin
Now, type the following:
jar cf MyJar.jar *.java Folder1 Folder2
javac *.java
jar cmf mymanifest.txt MyJar.jar *.class
--------------------------------------
Note: If you want to include stuff(pics,etc) from subfolders of the current folder, just give the name as shown in the first instruction above. If you don't want to include stuff, leave out Folder 1 etc.
The second instruction simply compiles the java files.
As for the last instruction, make sure that you have a text file called mymanifest.txt, and in the text file, have the instruction Main-Class: folowed by the name of the class that has the main method(public static void main())
e.g. Main-Class: MyProg
Leave a blank line at the end of the text file, then save it
You don't need to include .java
- 03-10-2009, 12:34 PM #9
Member
- Join Date
- Oct 2008
- Posts
- 58
- Rep Power
- 0
Darn. Previously, I pointed to a bin in netbeans that does not contain jar.exe. Now I found it in a "Java" folder. Wasted 2 days.
Now this is the trouble I am facing.
Look at the screenshot and see what is happeningLast edited by hitmen; 03-10-2009 at 12:50 PM.
- 03-10-2009, 01:12 PM #10
Member
- Join Date
- Oct 2008
- Posts
- 58
- Rep Power
- 0
Now there is another error message.
I ensured that there is a Main-Class: MainApp
with 2 lines before I closed the text file
Note that MainApp is the name of my main class
what is wrong?
- 03-10-2009, 02:08 PM #11
Member
- Join Date
- Oct 2008
- Posts
- 58
- Rep Power
- 0
- 03-10-2009, 03:05 PM #12
Member
- Join Date
- Oct 2008
- Posts
- 58
- Rep Power
- 0
Tell me what I must add in my manifest
Manifest-Version: 1.0
Created-By: 1.5.0_07 (Sun Microsystems Inc.)
Main-Class: CiphersApp
Is there a path I am suppose to add?
- 03-11-2009, 05:39 AM #13
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Generally, you will want a "Class-Path: " option in your manifest to specify other jar files your executable jar depends on, such as :
...and you might want a splash screen too, something like...Java Code:Class-Path: lib/a.jar lib/b.jar
Java Code:SplashScreen-Image: images/splash.jpg
- 08-27-2011, 11:30 AM #14
Member
- Join Date
- Aug 2011
- Posts
- 3
- Rep Power
- 0
hey i found out a link to forum
may be this could help you
How to create JAR file
Last edited by DarrylBurke; 08-27-2011 at 04:41 PM. Reason: Spam link removed
- 08-27-2011, 12:05 PM #15
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Similar Threads
-
Running applet from command prompt
By niteshwar.bhardwaj in forum Java 2DReplies: 1Last Post: 03-12-2009, 08:10 AM -
Is there any way to run java file through command prompt of .jar
By sachin.parnami in forum New To JavaReplies: 2Last Post: 12-03-2008, 01:24 PM -
help me!!!! about command prompt..
By kureikougaiji in forum New To JavaReplies: 2Last Post: 11-13-2008, 06:15 PM -
Regular expressions from command prompt
By GenkiSudo in forum New To JavaReplies: 2Last Post: 09-21-2008, 02:40 PM -
Problem during executing Command Prompt
By keshari in forum Advanced JavaReplies: 4Last Post: 06-05-2008, 04:06 AM


LinkBack URL
About LinkBacks


Bookmarks