i have tried to get help me nobody seems to understand my problem. I created a project in netbeans and it produces a jar file when i compiled it. It runs just fine on through the IDE but when i try to launch it from the command prompt it simply does not do anything. It just moves the cursor to the next line for another command? when i echo my classpath this is the result i get
/opt/netbeans-7.1.2/ide/modules/ext/mysql-connector-java-5.1.13-bin.jar:/h/USERS/local/pagola/NetBeansProjects/mylib/dist/mylib.jar:/h/USERS/local/pagola/NetBeansProjects/EOPPrototype/build/classes
i have added the classpath which contains everything that is within the jar. My manifest file contains the main class(entry point) and this is what it looks like
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_22-b04 (Sun Microsystems Inc.)
Class-Path: lib/mysql-connector-java-5.1.13-bin.jar lib/mylib.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: eopprototype.EOPPrototype
and so i cant figure out what i am doing wrong? do i need to add anything to my PATH variable (maybe the java jdk bin stuff) is it not able to locate the java command because it doe not know where to look for that executable?
NOTE: i did not setenv my PATH and LIBRARY PATH -do i need to do this ? this is a c style shell script
Try this first
If your Manifest does not end with a newline it maybe ignored. See manifest requires a newline
Otherwise I have to make assumptions
You don't need lib/mylib.jar in your manifest if that JAR file contains EOPPrototype with a public static void main(String[] args) method that you wish to execute. So remove it so it only lists mysql-connector-java-5.1.13-bin.jar. Your manifest will then look like this
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_22-b04 (Sun Microsystems Inc.)
Class-Path: lib/mysql-connector-java-5.1.13-bin.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: eopprototype.EOPPrototype
Then assuming all your code is in mylib.jar and the only other dependant jar is mysql-connector-java-5.1.13-bin.jar, create a directory structure like this
/h/USERS/temp/mylib.jar
/h/USERS/temp/lib/mysql-connector-java-5.1.13-bin.jar
Then execute like this
cd /h/USERS/temp
java -cp .:mylib.jar eopprototype.EOPPrototype
Related
I wanted to convert .java file to executable jar file,i did so in the command prompt. But everytime i double click (open) it nothing happens.
jar cf Calculate.jar Calculator.java
This is what i typed in the cmd. How to get an executable file? I use windows
That's not how it works, that way you are creating a jar archive with that .java file in it.
If you want to create the jar manually (if you want to do it one time, instead of using a build tool like ant or maven, it could have some educational value), you have to compile your .java file to a .class with javac, add it to the jar and then modify the META-INF/MANIFEST.MF manifest file to specify Calculator as value for the Main-Class attribute.
See this old guide from Sun.
How can i add external jars to my project before exporting the project so that the person about to use my code need not have to download those jars.He simply has to refer the jar i provided to run my project(Not using Maven).
Thanks
You might want to create a zip file containing
your_jar.jar
the libraries needed in a subdirectory lib
and add a manifest-classpath entry to point to those libraries
Manifest-Version: 1.0
Created-By: Hand
Main-Class: com.bitplan.gen.parser.m2java
Class-Path: lib/com.bitplan.smartgenerator-4.2.3.jar
After unzipping your code can be started with java -jar your_jar.jar
I want to create one executable JAR file which contains other JAR libraries. But when I execute the JAR, it gives an error of class path problem.
How do I solve the class-path problem?
I think you can try it like this;
Here is a simple example for you question. First, we assume we have a project directory like D:\javademo. In this working directory we then create a main class HelloWorld.java and thtat contains our other JAR files, like commons-lang.jar. Now, we must archive our main classes HelloWorld and commons-lang.jar into test.jar file.
First we must edit our manifest file so that we can specify our class-path and main-class
like this:
Manifest-Version: 1.0
Created-By: tony example
Class-Path: test.jar commons-lang.jar
Main-Class: org.tony.java.HelloWorld
We named this file test.mf. Now we use the jar command to generate our JAR file like this:
jar -cvfm test.jar test.mf -C ./ .
Then it will generate the JAR file test.jar. You can use this command to run this main class using java command:
java -jar test.jar
That is my solution. I hope it give you something helpful...
You should use third-party libraries for it. For example, OneJar. You'll have to build the final JAR file using the OneJar tool (like Ant task) instead of standard JRE's tools.
On running such a JAR file, OneJar's service class is launched instead of yours. This class then loads JAR files packed inside, as well as your classes, and run your main class.
There are similar questions and answers already. For example: Stack Overflow question Easiest way to merge a release into one JAR file.
I created simple Java Servlet: WelcomeServlet.java.
Than, I tried compile this file via:
javac WelcomeServlet.java
In result I see compile error:
package javax.servlet doesn't exit
I try find solution for this error with Google. And I find first part of answer: java compiler doesnt see servlet-api.jar file.
I know, that Apache Tomcat in it lib folder contains servlet-api.jar file.
So, I have this file, but where I must copy this file??
I try different folders:
echo %JAVA_HOME%
C:\Program Files\Java\jdk1.6.0_26
%PATH% contains this line: C:\Program Files\Java\jdk1.6.0_26\bin
So, I copy in:
%JAVA_HOME%\bin
%JAVA_HOME%\lib
%JAVA_HOME%\jre\lib
And in result same error.
And only after I copy servlet-api.jar in directory:
%JAVA_HOME%\jre\lib\ext
compilation complite sucessful.
My question: Why? Why I must copy in folder %JAVA_HOME%\jre\lib\ext ??
Where This moment describe in documentation?
And other question we have some official docs or specifications that describe folder structure for jdk folder??
You'll need to specify the directory or directories you want the compiler to search by using the -classpath command line option when running javac. The reason the compiler found your .jar in %JAVA_HOME%\jre\lib\ext is because it searches the extension directories by default.
This is for Java 1.5, but I believe it is more or less still correct:
http://docs.oracle.com/javase/1.5.0/docs/tooldocs/findingclasses.html
The link Shaun provides is a more complete answer. But in short, using the classpath is the best way to introduce 3rd party or external (to the JDK/JRE) libraries. The classpath is a concept much like the %PATH% or the $PATH variables, but specifies locations for java to use for lookup rather than the shell to use for lookup of executables.
The classpath provides the java compiler or java virtual machine a list of items to use when searching for resources. This "path" may include directories or files. It will typically include jar files and sometimes locations of configuration files. Many Java based lookup schemes for files configuration or otherwise use some variant of what is accomplished by [Class#getResourceAsStream()][1]'s use of walking the Classpath.
I have rarely seen an incident where putting a jar file in the lib/ext location was preferred to utilizing the Classpath.
The classpath is typically an environment variable (%CLASSPATH% or $CLASSPATH) or specified on the command line when running java or javac (e.g. -cp or -classpath see the help from the executable you are running).
Build tools such as Ant and Maven will also provide abstractions to defining the list of jars to be utilized by your applications and are highly recommended to be used for any length of repetitive change code, build, test, run cycles.
I have a client application that needs to run on Unix. It works fine in Windows but i get a NoClassDefFound exception in unix. Here's my manifest file:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 2.3 (IBM Corporation)
Main-Class: com.main.Client
Class-Path: lib/commons-lang-2.3.jar lib/commons-io-1.3.2.jar lib/comm
ons-logging-api-1.1.jar lib/log4j-1.2.12.jar
And the Client.jar structure:
com
com/main
lib
meta-inf
To run it, i use the command below:
$JAVA_HOME/jre/bin/java -jar Client.jar
It works fine in windows. Somehow i think that its not reading the manifest right. Help please! Thanks!
Maybe because your meta-inf folder is in lower-case?
One way is to make sure you have defined your CLASSPATH variable for the user that is running the program.
$ echo $CLASSPATH
if there is nothing shown from the above, then export it
$ export CLASSPATH="/lib/commons-lang-2.3.jar:/your/other/paths"