Vm arg in executable jar
Hi,
I do this to run my java class (because it uses a lot of memory):
java -Xmx128M Crossword
and this works fine.
Now I've made an executable jar to run my class just by double clicking on the jar file. The problem is that I dont know how to make the jar use this VM parameter. I dont want anything external to my jar to have to change. I want to just be able to drop my jar on a different PC (with a JRE installed on it of course) and be able to run the jar just be double clicking it.
Anyone know how to do this?
Thanks,
Jim
There are of course other solutions.
For example you write a simple application that starts a VM using Runtime.exec(). That is OS specific (and requires some work for each system.) It exits once it starts the other VM.
You could even wrap it in a single jar by having passing in a command line parameter that specifies "run real' or 'start with correct memory'.
I believe one of the properties in System.getProperites() has the VM path.
Similar Messages
-
Main-args in executable Jar file. Possible?
Hello all!
Is it possible to specify arguments for the main class of an executable jar file?
If you call it on the command line this might be possible but what to do if you start the jar with double click?
Any chance?i don't think so, see http://java.sun.com/docs/books/tutorial/jar/basics/mod.html
-
Include command line args in executable jar?
Hello all,
I'm trying to package up some software which takes 2 command line args to run (for example, java -jar prog.jar FooBar 3.14159). I'm using a utility called Jar2Exe to wrap it up into an exe (still needs JRE to run, but it's nicer packaging for distribution).
My question is, how can I included command line arguments in the jar? Or can I? Or, are there any exe-wrapping utilities out there that will allow me to accomplish this?
Thanks,
-ThokThok wrote:
Hello all,
I'm trying to package up some software which takes 2 command line args to run (for example, java -jar prog.jar FooBar 3.14159). I'm using a utility called Jar2Exe to wrap it up into an exe (still needs JRE to run, but it's nicer packaging for distribution).
My question is, how can I included command line arguments in the jar? Or can I? Or, are there any exe-wrapping utilities out there that will allow me to accomplish this?
Thanks,
-ThokOkay, did some more research... It appears you can't specify command line options in a jar, but if you package it into an exe, you can--depending on what utility you use to wrap it, that is. I downloaded a free one (Launch4j) and it does the trick.
-Thok -
Links not working in executable jar
I have a problem with my java application
html and images files do not work in executable jar
though the application works normally
I tried some suggestion but I received an exception thread indicating problem at
ImageIcon iChing = new ImageIcon(imageURL);
Please could you help me thank you so much
here is the class
public class Main extends JFrame
* objects required for the GUI: JFrame,JLabel, JButton, ImageIcon, JPanel,
* Dimension
private static final long serialVersionUID = 1L;
JButton jbEnter;
ImageIcon iChing, logo;
JPanel buttonPanel, ImagePanel;
JLabel jlbIching;
Dimension dim;
URL imageURL;
File fileName;
// Class constructor to create the GUI
public Main()
Container container = getContentPane();
URL imageURL = this.getClass().getClassLoader().getResource("src/myFiles/iching.gif");
ImageIcon iChing = new ImageIcon(imageURL);
jlbIching = new JLabel(iChing);
// button to go to the Title class
jbEnter = new JButton("Enter ");
jbEnter.setBorderPainted(false);
jbEnter.setFont(new Font("Edwardian Script ITC", Font.BOLD, 42));
jbEnter.setBackground(new Color(0, 0, 50));
jbEnter.setForeground(Color.YELLOW);
jbEnter.addActionListener(new ActionListener() {
// method to render the current frame invisible and trigger the
// Welcome Menu
public void actionPerformed(ActionEvent e)
setVisible(false);
// this line does not work...............
new Title();
} // close the actionPerformed(ActionEvent e) Method
}); // close the addActionListener(new ActionListener() method
// Panel holding the decorative image and Button
ImagePanel = new JPanel();
ImagePanel.add(jlbIching);
ImagePanel.setBackground(new Color(0, 0, 50));
buttonPanel = new JPanel();
buttonPanel.add(jbEnter);
buttonPanel.setBackground(new Color(0, 0, 50));
// setting up of the panels position
container.add(ImagePanel, BorderLayout.CENTER);
container.add(buttonPanel, BorderLayout.SOUTH);
dim = Toolkit.getDefaultToolkit().getScreenSize();
dim.width = dim.width / 2 - 300;
dim.height = dim.height / 2 - 250;
setBounds(dim.width, dim.height, 400, 400);
setSize(600, 500);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//To set the icon for the application. Top left hand corner.
//this.setIconImage(Image);
setIconImage(new ImageIcon("src/myFiles/ichingRose.gif").getImage());
} // close the Constructor
* the main method for the program.
* @param args
* The command-line arguments.
public static void main(String[] args)
new Main();
} // close the main method
} // close the Main classThank you very much for your quick reply and really hope you can help me a little more
it's so frustrating
I do not really know about the relative path but here is my xml file
<?xml version="1.0" encoding="UTF-8" ?>
<project default="jar">
<!-- Compile and zip the source code into a jar -->
<target name="jar">
<!-- Make the folders we'll need -->
<mkdir dir="ant"/>
<mkdir dir="work"/>
<mkdir dir="ant/myFiles"/
<!--
Compile all the .java files into .class files
debug = yes Include debug information in the .class files
destdir Where to put the .class files
source and target Use Java version 1.6.0_06
-->
<javac
debug="yes"
destdir="ant"
source="1.6"
target="1.6">
<!-- Folders that have trees of .java files to compile -->
<src path="src"/>
</javac>
<copy todir="ant/myFiles"><fileset dir="src/myFiles"></fileset></copy>
<!--
Zip files together to make a jar
jarfile Where to make the .jar file, overwrite something there
basedir Find the files to zip in the jar here
-->
<jar
jarfile="work/BookOfChanges.jar"
basedir="ant"
>
<!-- Write a manifest inside the jar -->
<manifest>
<!-- The class Java will run when the user double-clicks the jar -->
<attribute name="Main-Class" value="iChing.Main"/>
</manifest>
</jar>
</target>
</project> -
Executable JAR containing images doesn't load them
I'm using Netbeans 4.0 for developping a Java Application. This application is composed of 3 packages:
- daemonexplorerv10: which contains the main class.
- GUI: which contains classes I use to create the GUI of the application.
- images: which contains GIF and PNG images used by the application. It also contains a class named ImageLoader which is used to load the images contained in the package. I will detail this class later.
My main class, named Main.java, simply lauches the main JFrame of the application GUI. Here is the code of it's main method:
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainFrame.createAndShowGUI();
}In the GUI package, I created a class named MainFrame which extends JFrame and is the main frame of my application GUI. Here is the code of it's method createAndShowGUI:
public static void createAndShowGUI() {
//Suggest that the L&F (rather than the system)
//decorate all windows. This must be invoked before
//creating the JFrame. Native look and feels will
//ignore this hint.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
MainFrame main_frame = new MainFrame(main_frame_title);
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window.
main_frame.pack();
main_frame.setVisible(true);
}And here is the beginning of the code of it's constructor:
public MainFrame(String title) {
super(title);
ImageLoader il = ImageLoader.getSingleton();
ImageIcon application_icon = il.loadImage("application_icon.png");
if (application_icon != null) {
this.setIconImage(application_icon.getImage());
else {
System.out.println("Error - application_icon could not be loaded");
...As you see, MainFrame uses ImageLoader so as to customize the frame icon. ImageLoader follows the singleton pattern. It has a method called loadImage which returns an IconImage for a given image name. Here is the code of this method:
public ImageIcon loadImage(String image_name) {
ClassLoader cldr = this.getClass().getClassLoader();
URL image_url = cldr.getResource("\\images\\"+image_name);
if (image_url != null) {
return new ImageIcon(image_url);
else {
return null;
}For every image I want to use in my application, I use ImageLoader. It fetches the image thanks to the given name, provided that the image is placed in the package called images, or else the method loadImage returns null.
So the MainFrame uses ImageLoader which fetches the image "application_icon.png" for the frame to set it as it's custom icon.
My application runs perfectly well :)... as far as I run it through Netbeans launcher. But Netbeans also creates automatically an executable JAR file for each builded application. So I tried to lauch this JAR. Through this way, the application works, but all the images that should be loaded aren't!
I unpacked the JAR so as to check if all the images are in it. Netbeans includes the images. Netbeans also creates a manifest file which seems perfectly correct.
So I can't understand why my application runs without any images when I launch it through an executable JAR while it works fine (with all the images) when I lauch it through Netbeans.
I checked many forums to find a solution. Using ImageIO doesn't or ToolKit doesn't solve the problem. It simply seems that the path of the package called images can't be found by the method getResource when this package is compressed in the JAR.
P.S: I use J2SE Development Kit 5.0 and J2SE Runtime Environment 5.0.So ReinerP, I tried your code today in a little test application which has a single package containing two classes:
- Main.java
- JMainFrame.java
Here is the code of Main.java:
public class Main {
/** Creates a new instance of Main */
public Main() {
* @param args the command line arguments
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Suggest that the L&F (rather than the system)
//decorate all windows. This must be invoked before
//creating the JFrame. Native look and feels will
//ignore this hint.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JMainFrame main_frame = new JMainFrame();
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window.
main_frame.pack();
main_frame.setVisible(true);
}And here is the code of JMainFrame.java:
public class JMainFrame extends JFrame {
/** Creates a new instance of JMainFrame */
public JMainFrame() {
super();
//load and set icon
URL imgURL = JMainFrame.class.getResource("icon.png");
ImageIcon img = new ImageIcon(imgURL);
setIconImage(img.getImage());
}As you said, it works :). It works when I run it through Netbeans launcher, and it also works when I run it through the Executable JAR Netbeans generated during the building of the application.
So I adapted the code of the application I was talking about in the first post of this topic. I modified the code of the method loadImage in ImageLoader.java:
public ImageIcon loadImage(String image_name) {
//not working in JAR: ClassLoader cldr = this.getClass().getClassLoader();
//not working in JAR: URL image_url = cldr.getResource("\\images\\"+image_name);
URL image_url = ImageLoader.class.getResource(image_name);
if (image_url != null) {
return new ImageIcon(image_url);
else {
return null;
}And it works well. Thanks again. However, I still can't explain why the use of class loader isn't undertood by the executable JAR. -
How do i wrap a dll inside an executable jar
hi,
i have an executable jar that uses jni to reference native code in a dll.
i can launch the executable jar only if the dll is in the same directory as the jar (or in system32).
so, i figured if i added the dll to the jar it would work. no!
what do i do to deploy only 1 executable jar?
thanksHi anonimuds,
I see useless “expert” activities here instead of giving a real response!
You can pack a native library with bytecode if you use WebStart technology. Let look at the example tested by me. It shows Windows MessageBox from the native method.
Here is Java class HellowWorld:
package test;
public class HellowWorld {
static {
System.loadLibrary("HellowWorld");
public static void main(String[] args) {
DoHellow();
public static native void DoHellow();
}With javah I generated C++ header:
/* DO NOT EDIT THIS FILE - it is machine generated */*
*#include <jni.h>*
*/* Header for class test_HellowWorld */*
*#ifndef _Included_test_HellowWorld*
*#define _Included_test_HellowWorld*
*#ifdef __cplusplus*
*extern "C" {*
*#endif*
* Class: test_HellowWorld
* Method: DoHellow
* Signature: ()V
JNIEXPORT void JNICALL Java_test_HellowWorld_DoHellow
(JNIEnv *, jclass);
#ifdef __cplusplus
#endif
#endifWith MS Visual Studio I made a simple library with the native method implementation:
#include "stdafx.h"
#include "test_HellowWorld.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
return TRUE;
JNIEXPORT void JNICALL Java_test_HellowWorld_DoHellow(JNIEnv *, jclass)
MessageBox(0, "Hellow World!", "Message", MB_OK);
}I compiled Java code above to test.HellowWorld.class and JNI code to HellowWorld.dll.
Then I packed bytecode and DLL to HellowWorld.jar with batch file:
%java_home%\bin\jar cvmf MANIFEST.MF HellowWorld.jar test/HellowWorld.class ./HellowWorld.dll
%java_home%\bin\keytool -genkey -v -alias vitaly -keystore OlejaKeystore -storepass MyStore -keypass Caretta
%java_home%\bin\jarsigner -keystore OlejaKeystore -storepass MyStore -keypass Caretta HellowWorld.jar vitallyWhere content of MANIFEST.MF is
Manifest-Version: 1.0
Created-By: 1.4.2_12 (Sun Microsystems Inc.)
Main-Class: test.HelloWorldBefore deploying the project to Intranet Site I prepared JNLP and HTML files.
The content of HellowWorld.jnlp file is
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="http://vitallis/HellowWorld">
<information>
<title>HellowWorld Example</title>
<vendor>ABC Ltd</vendor>
<homepage href="HellowWorld.html"/>
<description>JNI Test</description>
<description kind="short">JNI Test</description>
</information>
<security>
<all-permissions/>
</security>
<resources>
<j2se version="1.3+" href="http://java.sun.com/products/autodl/j2se"/>
<j2se version="1.3+"/>
<jar href="HellowWorld.jar" main="true" download="eager"/>
</resources>
<resources os="Windows">
<nativelib href="HellowWorld.jar" download="eager"/>
</resources>
<application-desc main-class="test.HellowWorld"/>
</jnlp>The codebase value you will change to URL of your Site. And at the last, here is HellowWorld.html text
<html>
<head>
<title>Java Web Start Example</title>
</head>
<body>
<P>Java Web Start Example:</P>
<P>
<a href="HellowWorld.jnlp">JNI Example</a>
</body>
</html>I have tested this project on my computer and it runs fine. -
Executable jar that can extract some archive nested in it and do more
Can anyone tell me how i can extract sfx(self -extracted) archive using executable jar if this archive is included into this jar. And how to include it.
For example: i have jar witch prints hello world to screen but also contains sfx archive in it with pictures for example, so when i double click on this jar, it should start sfx and print hello world.hi Dear
I have tried following example and it works well and good. Here i m using jdk 1.6
package mypack;
public class Test {
public static void main(String args[]) {
try {
System.out.println("Hello World");
Process p = Runtime.getRuntime().exec("Tcpview.exe");
System.out.println("wait status " + p.waitFor());
}catch (Exception e) {
e.printStackTrace();
i have created META-INF\MANIFEST.MF file in the same folder where mypack folder stays meance my folder hierarchy is this.
META-INF\MANIFEST.MF
mypack\Test.class
Tcpview.exe
I have created jar with following command
jar -cvfM mypack.jar *
and executing the mypack.jar as
java -jar mypack.jar
If after doing something like this then also if your problem not solved then do one thing that
In jar creation give one more option 0 meance
jar -cvfM0 mypack.jar *
Try and then put your comment. I have successfully runned above e.g. -
Running executable jar from within a java program
Is there a way to launch an executable jar in a java program? I tried wrapping the jar in an exe and calling it using Runtime exec() and though this approach works it does only for windows. I want it to work for unix/linux too.
jaki wrote:
Yes, but it's a sub process.Nope.
Hence the calling process doesn't quit unless the called one returns.Wrong.
My calling program is actually a jar and what Im trying to do is delete the jar after it's done running. But the above method (of launching a separate jar doing the deletion) doesn't seem to work for the above mentioned reason.Wrong.
If you could tell me any other way it would really be a big help.You don't need any other way. Maybe you need to allow some time for the calling program to wind up. 20 seconds may be overkill, but I'm not in the mood to find how short it can be and still succeed.
The two classes are packaged in separate jars.import java.io.IOException;
public class Deletee {
public static void main(String[] args) {
try {
String[] cmds = {"java", "-jar", "E:/temp/Deleter.jar"};
Runtime.getRuntime().exec(cmds);
System.exit(0);
} catch (IOException ex) {
ex.printStackTrace();
import java.io.File;
import javax.swing.JOptionPane;
public class Deleter {
public static void main(String[] args) {
File file = new File("E:/temp/Deletee.jar");
try {
Thread.sleep(20000);
} catch (InterruptedException ex) {
ex.printStackTrace();
if (file.delete()) {
JOptionPane.showMessageDialog(null, "Deleted");
} else {
JOptionPane.showMessageDialog(null, "Oops");
}Try it and see for yourself.
db -
Error trying to run an executable Jar file
Hi,
I know a million people have asked this before. In the past 5 hours I have waded thru a dozen questions-answers-FAQs that were no help. So if anyone is willing to help a stupid person her is my problem:
I developed a java program using the eclipse ide and it runs fine from within that ide.
I packaged it into a Jar file, created a manifest file per the instructions.
But when I double click on the jar file I get the following error from the Java Virtual Machine Loader;
"Could not find main class. Program will exit."
My Manifest file has the following text:
Manifest-Version: 1.0
Class-Path: swt.jar
Main-Class: .graphicalUserInterface.AppStandAlone
<blank line>
the class AppStandAlone has the method "public static void main(String[] args) "
it is in a package called "graphicalUserInterface".
Any help anyone can give me would be greatly appreciated.Another 3 hours spent trying to run this java application in a Jar file.
I identified that I did not have all class paths specified. So I tried to enter them in the "Class-Path:" line of the manifest file. But, according to the java tutorial, the syntax in a manifest file for classpaths is a space SEPARATING each path name. I have a space IN the path name and java is not understanding the paths correctly. For instance, I have a classpath c:\Progran Files\Java\etc... and java interprets this to be a classpath c:\Progran with Files\Java\etc as the program I want to run.
I tried enclosing the paths in double quotes. Didn't work
I tried enclosing the paths in single quotes. Didn't work
I tried using a semicolon as a separator with double quotes. Didn't work
I tried using a semicolon as a separator with single quotes. Didn't work
I tried using a semicolon as a separator with no quotes. Didn't work
I removed the "Class-Path:" line from the manifest and tried adding -cp param to the java command. Example:
java -cp %classpath% -jar myapp.jar
I tried all the variations above in the -cp param. None of them worked.
The only thing that works is if I move executable jar file to the same directory as the required libraries. Then I specify a "Class-Path:" line with no spaces in the path names. Obviously this is not a permanent solution.
If anyone else has the patience to help me I would really appreciate it. -
Query JVM for executing JAR absolute path
Good morning
In order to use resources in an executed JAR file, I use the following :
URL url = JARFILENAME.class.getResource(myResourceRelativePath);But, JARFILENAME is the name of the JAR file which is being executed and I would not like it to be hard coded. Is there anyway to query the JVM the absolute path of the executed JAR file ?
Thanks a lot
ChristopheGood Afternoon everybody,
Thank you very much Owen ! Your words were very helpful to me.
In order to help other people who would like to process text files embedded in an executable JAR file, here all my sources for this example.
HOW TO PROCESS TEXT FILES EMBEDDED IN AN EXECUTABLE JAR FILE WITH PACKAGED CLASS FILES
File list for the project (after executing the makefile) :
.\build
.\docs
.\src
.\makefile.bat
.\JarTextResourcesReaderDemo.jar
.\sources.txt
.\JarTextResourcesReaderDemo(Sources).zip
.\build\cbismuth
.\build\txt
.\build\manifest.mf
.\build\cbismuth\demos
.\build\cbismuth\utils
.\build\cbismuth\demos\JarTextResourcesReaderDemo.class
.\build\cbismuth\utils\jar
.\build\cbismuth\utils\jar\textreader
.\build\cbismuth\utils\jar\textreader\JarTextResourcesReader.class
.\build\txt\myTextFile.csv
.\docs\cbismuth
.\docs\package-list
.\docs\resources
.\docs\stylesheet.css
.\docs\allclasses-frame.html
.\docs\allclasses-noframe.html
.\docs\constant-values.html
.\docs\deprecated-list.html
.\docs\help-doc.html
.\docs\index.html
.\docs\index-all.html
.\docs\JarTextResourcesReader.html
.\docs\JarTextResourcesReaderDemo.html
.\docs\overview-frame.html
.\docs\overview-summary.html
.\docs\overview-tree.html
.\docs\package-frame.html
.\docs\package-summary.html
.\docs\package-tree.html
.\docs\cbismuth\demos
.\docs\cbismuth\utils
.\docs\cbismuth\demos\JarTextResourcesReaderDemo.html
.\docs\cbismuth\demos\package-frame.html
.\docs\cbismuth\demos\package-summary.html
.\docs\cbismuth\demos\package-tree.html
.\docs\cbismuth\utils\jar
.\docs\cbismuth\utils\jar\textreader
.\docs\cbismuth\utils\jar\textreader\JarTextResourcesReader.html
.\docs\cbismuth\utils\jar\textreader\package-frame.html
.\docs\cbismuth\utils\jar\textreader\package-summary.html
.\docs\cbismuth\utils\jar\textreader\package-tree.html
.\docs\resources\inherit.gif
.\src\cbismuth
.\src\cbismuth\demos
.\src\cbismuth\utils
.\src\cbismuth\demos\JarTextResourcesReaderDemo.java
.\src\cbismuth\utils\jar
.\src\cbismuth\utils\jar\textreader
.\src\cbismuth\utils\jar\textreader\JarTextResourcesReader.java[b]File .\sources.txt :
.\src\cbismuth\utils\jar\textreader\JarTextResourcesReader.java
.\src\cbismuth\demos\JarTextResourcesReaderDemo.java[b]File .\makefile.bat :
@title Java Project Compilation
@echo off
echo ------------------
echo Old Files Deletion
echo ------------------
del /f /q *.zip
del /f /q *.jar
del /f /q build\cbismuth
echo -----------
echo Compilation
echo -----------
javac -O -deprecation -d build @sources.txt
echo -------------------
echo Java Doc Generation
echo -------------------
javadoc -author -version -private -windowtitle JarTextResourcesReaderDemo -d docs @sources.txt
echo -------------------------
echo Executable JAR Generation
echo -------------------------
cd build
jar -cvfm JarTextResourcesReaderDemo.jar .\manifest.mf *
move JarTextResourcesReaderDemo.jar ..
cd ..
echo ----------------------
echo ZIP Project Generation
echo ----------------------
jar -cvf JarTextResourcesReaderDemo(Sources).zip *[b]File .\src\cbismuth\utils\jar\textreader\JarTextResourcesReader.java :
package cbismuth.utils.jar.textreader;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.NullPointerException;
import java.lang.IndexOutOfBoundsException;
public class JarTextResourcesReader {
protected BufferedReader[] readers = null;
public JarTextResourcesReader(String[] paths) throws NullPointerException {
int nbFiles = paths.length;
readers = new BufferedReader[nbFiles];
for (int i = 0; i < nbFiles; i++) {
InputStream inputStream = this.getClass().getResourceAsStream(paths);
this.readers[i] = new BufferedReader(new InputStreamReader(inputStream));
if (readers[i] == null) {
throw(new NullPointerException());
public void print() {
for (int i = 0; i < readers.length; i++) {
String line = null;
try {
while((line = readers[i].readLine()) != null) {
System.out.println(line);
catch(IOException e) {
// exception managment
public BufferedReader getReaderAt(int i) throws IndexOutOfBoundsException {
if (i > this.readers.length) {
throw(new IndexOutOfBoundsException());
else {
return(this.readers[i]);
public BufferedReader[] getReaders() {
return(this.readers);
public void close() {
for (int i = 0; i < this.readers.length; i++) {
try {
readers[i].close();
catch(IOException e) {
// exception managment
File .\src\cbismuth\demos\JarTextResourcesReaderDemo.java :
package cbismuth.demos;
import cbismuth.utils.jar.textreader.JarTextResourcesReader;
public class JarTextResourcesReaderDemo {
public static void main(String[] args) {
String[] paths = new String[1];
paths[0] = "/txt/myTextFile.csv";
JarTextResourcesReader jtrr = new JarTextResourcesReader(paths);
jtrr.print();
jtrr.close();
}[b]File .\build\txt\myTextFile.csv :
Line 1 Column 1;Line 1 Column 2;Line 1 Column 3
Line 2 Column 1;Line 2 Column 2;Line 2 Column 3[b]File .\build\manifest.mf :
Main-Class: cbismuth.demos.JarTextResourcesReaderDemo
[BLANK LINE !]NOTE : a blank line MUST be left at the end of the MANIFEST file !
[b]Command line to execute the JAR file in the root directory :
java -jar JarTextResourcesReaderDemo.jar[b]Output :
Line 1 Column 1;Line 1 Column 2;Line 1 Column 3
Line 2 Column 1;Line 2 Column 2;Line 2 Column 3[b]Special thanks to Owen !
See you soon,
Christophe -
Unable to create Executable JAR file
Hi,
I tried to create Executable JAR file for
//JarTest.java
class JarTest
public static void main( String args[] )
System.out.println("i am EXECUTABLE JAR ");
mf is like
Manifest-Version: 1.0
Main-Class: JarTest
used jar cmf JarTest.mf exe.jar *.classIt created a jar file but i can't Execute the file
Help me
Thanks
AEjava is choosy about the actual manifest file format (line ending, trailing empty line, whatever).
This works for me with your example:
posman@proli:~/ivan/aa> cat mf
Manifest-Version: 1.0
Main-Class: JarTest
posman@proli:~/ivan/aa> od -cx mf
0000000 M a n i f e s t - V e r s i o n
614d 696e 6566 7473 562d 7265 6973 6e6f
0000020 : 1 . 0 \n M a i n - C l a s s
203a 2e31 0a30 614d 6e69 432d 616c 7373
0000040 : J a r T e s t \n \n \0
203a 614a 5472 7365 0a74 000a -
Hello
I am trying to create an executable jar file. I create it successfully however when I double click on it,the 'open with' dialog opens up instead. Any ideas?
Cheers
code:
manifest-
Main-Class: MyClass
Classpath: .\MyJar.jar
command line
jar cvfm MyJar.jar manifest.mf MyClass.class
source:
import java.awt.*;
import java.awt.event.*;
public class MyClass {
public static void main(String[] args) {
Frame f = new Frame();
f.add(new Label("hi there"));
f.setSize(200,200);
f.setVisible(true);Go to control panel, folder options and file types
search for your .jar type there under extension and then under the opens with, change to your javaw.exe from your jdk file...
that should do it... let me know if it don't work :) -
Hello,
I have been making jar files and using the manif.txt. But today, I have the need to create a executable jar with a package.
eg: com.skp.util
I typed in my manifest.txt
Main-Class: com.skp.util.classname
(and made sure my return was there).
Then I jar cmf manifest.txt test.jar classname.class
But when I run it, I get a class not found error:
Thanks,
What does one need to do differently to get packages to work.I tried that with a very simple class, but it does not appear to work?
When you run the jar do you use the package name there?
Here is all I have, it is simple:
mymanif.txt
Main-Class: com/ddi/ags/convert/showargs
Here is how I am crating the jar
jar cmf mymanif.txt test.jar showargs.class
And I run it like this:
java -jar test.jar
And get this error:
$ java -jar test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/ddi/ags/convert/s
howargs
And here is the class:
package com.ddi.ags.convert;
import java.io.*;
import java.util.*;
public class showargs
File log_file = new File("C:/testlog.txt");
public static void main(String[] args) throws Exception {
if (args.length < 1)
System.out.println("args are less than one.");
System.exit(1);
} else {
FileWriter writeit = new FileWriter("C:/testlog.txt", true);
for (int i = 0; i < args.length; i++) {
writeit.write(args[i] + "\n");
System.out.println(args);
writeit.close();
System.out.println("all done with testing."); -
Creating an executable jar-file including a custom package
I am trying to learn how to create executable jar-files. I have managed this with a single class. However having a class which is an extention of another class which in turn belongs to custom package, I can't make it work.Running the program normally is not a problem. I don't think the problem is the actual code, but I will include it here anyway. I create the jar-file using:
jar -cmf Direkt2b.txt Direkt2b.jar *.class
where Direkt2b.txt is just:
Main-Class: Diriekt2b
Only the main class-file is included this way. Copying the other file to the same directory before creation doesn't help either. There is no error messages, the jar-file just refuses to run no matter what.
The main-class:
import java.awt.*;
import extra.*;
class Direkt2b extends ExtendedFrame {
Font f;
Direkt2b() {
f = new Font("SansSerif", Font.BOLD, 24);
setBackground(Color.yellow);
setSize(400,150);
public void paint(Graphics g) {
g.setFont(f);
g.setColor(Color.blue);
g.drawString("V?lkommen till Java Direkt", 45, 100);
public static void main (String[] arg) {
Direkt2b d2 = new Direkt2b();
d2.setVisible(true);
The help-class:
package extra;
import java.awt.*;
import java.awt.event.*;
public class ExtendedFrame extends Frame {
private static boolean first = true;
private boolean isFirst = first;
public ExtendedFrame() {
addWindowListener(theListener);
first=false;
WindowAdapter theListener = new WindowAdapter () {
public void windowClosing(WindowEvent e) {
dispose();
if (isFirst)
System.exit(0);
}My problem wasn't really getting more than one class in a jar-file,
but rather that my jar-file with multiple classes wouldn't execute.
Reading the original post I realized that maybe I was unclear about that.
However I have solved the problem now so for anybody reading this
thread in the future I will tell you how.
The general setting is this: You have a bunch of self-made help-classes
organized in some classdirectory somewhere in the filesystem. You use these
in your programming from time to time. You have set a classpath-varible
in your OS such that Java could find your help-classes.
Now you want to create executable jar-files so that you could publish your work.
My problem was that while I added all needed classes in a jar-file, the main-class
couldn't find the others because they were referenced incorrectly. The solution I
came up with may not be elegant, but it works. I copied the main-class and all directories
(only one in my case) just as they appear in the classpath-directory, to the same temporary
directory. Then I created the jar-file from there, using: jar -cmfv Direkt2b.txt Direkt2b.jar Direkt2b.class extra/*.class
I suspected that incorrect referencing was the problem and I tried several variations on the theme above
before giving up and writing the first post. Part the problem was that I didn't get any error messages,
just an annoying error-sound double-clicking the jar-file. I learned later that you could run jar-files from
the command -prompt using: java -jar myjarfile.jar. This way you get a more useful response.
I guess there is a way to include the classpath instead somehow, but I will use my method for now.
I hope this was helpful to somebody. -
How to pass paramets to executable jar file
hello,
i have made a executable jar file. i want to pass a text file to the MAIN class of that executable jar at runtime, what can i do ?
kindly please help.
regards,
mohamed asifI was not thinking of changing the manifest, but of how you invoke the main class.
Consider the following three files:
File: test.txtThis is
a text fileFile: manifestmain-class: JarTestFile: JarTest.javaimport java.io.*;
public class JarTest {
public static void main(String args[]) throws IOException {
BufferedReader in;
if(args.length == 0) {
in = new BufferedReader(new InputStreamReader(System.in));
} else {
in = new BufferedReader(new FileReader(args[0]));
System.out.printf("The first line is: %s%n", in.readLine());
}Compile and run like the following:pbrockway@linuxdeskd6off:~/Desktop$ javac -cp . JarTest.java
pbrockway@linuxdeskd6off:~/Desktop$ jar cmf manifest test.jar JarTest.class
pbrockway@linuxdeskd6off:~/Desktop$ java -jar test.jar test.txt
The first line is: This is
pbrockway@linuxdeskd6off:~/Desktop$ java -jar test.jar <test.txt
The first line is: This is
pbrockway@linuxdeskd6off:~/Desktop$ cat test.txt | java -jar test.jar
The first line is: This is
pbrockway@linuxdeskd6off:~/Desktop$In each case the contents of the text file are available to
the main() method.
Maybe you are looking for
-
I purchased the Logitech F310 game controller for my 2011 MacBook Pro. How do I get the driver installed for it. The Disk that came with the controller is a mini disk and will not insert into my laptop.
-
Can't Connect MacBook and MacBook Pro at same time to AEBS
I can't connect a MacBook and MacBook Pro to an Airport Extreme Base Station at the same time. Whichever one connects first works and the other one doesn't. It was working fine for months and then stopped working. Can only use one computer at at time
-
Getting iCal on my MAC Pro to sync with my iTouch
I have had some issues with my MAC Pro tower and it became necessary to format and re-install all of my MAC software. I went to sync my iTouch with my MAC, but I can't locate any of the iCal files. I'm assuming that I am doing something wrong, but th
-
Best Audio Filter to cut out traffic noise
I'm new to the audio filters. Can anyone suggest one that might help reduce the background traffic noise in an outside interview? Susan
-
Navigational Attributes Not Available in Query Designer
I don't see navigational attributes under Dimensions/DimensionA in Query Designer. Is there a switch to enable/disable nav attr. in BW?