extracting portion of jar to a path - java

I am using the follwing command and my intention is to extract only DYEDistinctAppServer.topology from
discovery1-full-8.1.0-07-10-2010_1055.jar at data/product/template-topologies/DYEDistinctAppServer.topology path.
Command:
jar -xf discovery1-full-8.1.0-07-10-2010_1055.jar -C data/product/template-topologies/DYEDistinctAppServer.topology
Instead of extracting the files, it prompts me that I am using the command incorrectly and dumps out the help with all the various options.
Is there any syntax error as it is shown in the usage part.
I am using AIX OS.
Thanks.

The option -C is to set the directory in which jar should run, not paths in the JAR file. Try:
jar -xf discovery1-full-8.1.0-07-10-2010_1055.jar data/product/template-topologies/DYEDistinctAppServer.topology

Related

Java Program Not Starting

I'm currently trying to launch a Java program (JTS3ServerMod, a Java TeamSpeak bot), using the included start script. However, running the start script presents a Java error and I'm not sure why.
I am currently running the Oracle JDK (with JRE) and all of the relative paths and options are in place. Here is the what I see in terminal after the script trys to execute java -jar:
[root#s1 JTS3]# ./jts3servermod_startscript.sh start
For security reasons it is prefered not to run the JTS3ServerMod as root!
jts3servermod.pid found, but no JTS3ServerMod running. Possibly your previously started JTS3ServerMod crashed!
Please view the logfile for details.
Starting the JTS3ServerMod...
JTS3ServerMod started, for details please view the log file!
[root#s1 JTS3]# Illegal option: j
Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Options:
-c create new archive
-t list table of contents for archive
-x extract named (or all) files from archive
-u update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information from specified manifest file
-n perform Pack200 normalization after creating a new archive
-e specify application entry point for stand-alone application
bundled into an executable jar file
-0 store only; use no ZIP compression
-P preserve leading '/' (absolute path) and ".." (parent directory) components from file names
-M do not create a manifest file for the entries
-i generate index information for the specified jar files
-C change to the specified directory and include the following file
If any file is a directory then it is processed recursively.
The manifest file name, the archive file name and the entry point name are
specified in the same order as the 'm', 'f' and 'e' flags.
Example 1: to archive two class files into an archive called classes.jar:
jar cvf classes.jar Foo.class Bar.class
Example 2: use an existing manifest file 'mymanifest' and archive all the
files in the foo/ directory into 'classes.jar':
jar cvfm classes.jar mymanifest -C foo/ .
Turns out, that for some reason, the system started trying to use a version of Java that no longer existed (hence the errors). I simply made the system look in the right place again, and all is well.
I have faced similar problem before I have injected into jar manually did you do something like that by any chance ? you might need to correct the paths in the jar if so

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

Compile and execute java via bash shell

I'm trying to write a shell code that will execute my Java program. I tried this:
#!/bin/sh
COMPILECMD="javac server.jar"
RUNCMD="java -jar server.jar 12777"
$COMPILECMD
$RUNCMD
But I got this error:
error: Class names, 'server.jar', are only accepted if annotation
processing is explicitly requested 1 error Failed to load Main-Class
manifest attribute from server.jar
In the command line I don't use the jar file, I just compile and then execute:
javac server.java
java server 12777
The typical way to build a Java program into a JAR file from the shell is to use the ant utility.
Its home (and documentation) is here: http://ant.apache.org/
Essentially, you'd write (or use a tool to write) a build.xml in your project folder, and then run ant to compile, bundle, et al.
(Disclaimer: I'm not a fan of Ant, but it is, I believe, the most common/popular tool in the Java universe for this task.)
You can also use traditional Unix Makefiles, if you're familiar with them. Some rules like:
CLASSES= Server.class Supporting.class
%.class: %.java
javac $<
%.jar: $(CLASSES) Manifest.mf
jar cfm $# Manifest.mf $(CLASSES)
You could also use a straight-ahead shell script:
javac Server.java
javac Supporting.java
jar cfm Server.jar Manifest.mf Server.class Supporting.class
However, maintaining this is liable to be a nightmare if your project grows beyond a few files.
There are other tools available, as well, but these are the most typical ones I know of.
I think you get wrong understanding.
jar is created by jar command not by javac command.
jar command create a jar file (zip file containing all the class inside)
javac command just compile and transform .java to .class only
Example compile-command:
javac server.java; jar cvf server.jar server.class
You may find the following link helping.
http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

java classpath in unix

I can run java in cygwin+windows using the following settings (the sw/jar directory has several jar files, and I pick the relevant one from the java command line):
CLASSPATH=.;C:\sw\java_6u35\lib\\*;C:\sw\jar\\*
java org.antlr.Tool Calc.g
But I am having the following problems when running in linux:
(1) I can't set a directory name in a classpath, the following line reports an error:
setenv CLASSPATH .:/sw/jdk1.6.0_35/lib/\*:/sw/jar/*
(2) when I run explictly with -jar option, I still get an error:
java -jar /sw/jar/antlr-3.4.jar org.antlr.Tool Calc.g
error(7): cannot find or open file: org.antlr.Tool
However, the class does exist. When I do jar tf /sw/jar/antlr-3.4.jar, I get:
...
org/antlr/Tool.class
So my question is: (a) how do I specify in unix that my jar-directory is xxx that contains several jar files, and (2) how do I pick the relevant jar from this dir at runtime?
To specify multiple jars in a directory, directly in the java command, use this
java -cp "/sw/jar/*" org.antlr.Tool Calc.g
This will include all the jars
If you want to set the classpath in Unix/Linux systems, use this
export CLASSPATH=/sw/jar/a.jar:/sw/jar/b.jar
in unix use this to set the classpath:
export CLASSPATH=myClassPath
about not finding your jar, you're using a leading slash (/), that means that you path is absolute (not relative to your home folder) is this what you want?
if you want the path to be relative to your folder try:
java -jar ~/mypathToMyJar

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