jar not reading input file directory recursively - java

I'm having trouble running the jar command in cygwin. The input-files parameter isn't treating the directory I'm passing it recursively when I'm referencing it with ".."s in my path.
For example, I'm running this in the same directory as the "src" directory. src/ contains my package structure of class and java files. This runs properly and creates a jar containing my source and class files.
jar cf jarname.jar src
However when I run this next command, I get an empty jar except for a manifest file.
jar cf jarname.jar localdir/../src
I need to run this from a script that needs to find this directory with a ".." directory so I need the 2nd command to work.
Anybody know why this isn't working or have a workaround? I tried using realpath but it complains that it can't find that path at all. I may be using it wrong though.

The Directory path in cygwin is different . To navigate to any drive for example to C drive we need to type in:
/cygdrive/c
A very easy work around i found useful is just to type cmd in the terminal . This allows you to use the actual path than the cygwin specific path .
try typing cmd and then running the command , it worked for me

Related

Create jar command doesn't find file

build> jar cvfe test\MyJavaLibrary.jar Main -C test\java Main.class foo\SomeClass.class
foo\SomeClass.class : no such file or directory
added manifest
adding: Main.class(in = 444) (out= 308)(deflated 30%)
The corresponding directory structure is
build/
test/
java/
foo/
SomeClass.class
Main.class
It is very strange that it works correctly when using test\java\foo\SomeClass.class as the last argument instead. Fine, but that doesn't work when I do the same for the first argument, that is test\java\Main.class. The behaviour appears to be very inconsistent.
Why can it not find foo\SomeClass.class, or better, what is the right way to create the Jar?
java version "1.8.0_102"
The executable that you're running from the command-line appears to start in the build directory, and as such, it doesn't know where the foo\SomeClass.class file is relative to build - this would also explain why test\java\foo\SomeClass.class works (it knows how to find the file relative to the execution directory).
If you were to have the foo folder on the top level inside build, I believe it would work correctly, but because foo is nested within two layers of directories, the executable has no idea where to look for a folder with that name in its current directory.
I just found the answer in a documentation page I haven't seen before.
-C dir
Temporarily changes directories to dir while processing the following inputfiles argument. Multiple -C dir inputfiles sets are allowed.
So apparently the -C argument would need to be placed for every input, not just specified once.
Source: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jar.html

java command line argument to make jar find files in two directories?

I have a jar file which I do not have the source code but want to use.
The jar file prompts for a file to read and generates an output file using a combinatin of the input file and a number of 'helper' files it uses for data. It works perfecty fine if run from its expected home directory, but I'm trying to write a script which will allow running the jar from anywhere.
The problem is that if I try running the jar file from anywhere other then its home directories it fails to find the support files it needs to properly generate its data.
If I run the file from its expected home directory I have to give the full address of the input file or it won't find it. I would prefer to be able to give just the relative path and Java know to look at whatever directory the person calling my script is in.
Is there a way I can have a bash script pass a command line argument to Java that would ensure that this jar looks at both of the relevant directories (directory of the helper files and the current dir of the person calling the script) when trying to resolve a relative file path? Something like the -classpath argument?
With the --classpath (or -cp) you can tell your Java program where it should take the dependency classes. So, probably if you do like in your files directory
$JAVA_HOME/bin/java -cp '.:/path/to/the/original/program' My.class myfile.txt
then it will wind the program, and find your files as well.
UPDATE
If it doesn't work, you can try to force the file loading some other way. The Javadoc says:
By default the classes in the java.io package always
resolve relative pathnames against the current user directory. This
directory is named by the system property user.dir, and
is typically the directory in which the Java virtual machine was
invoked.
So, you can try running the program from the original directory this way:
$JAVA_HOME/bin/java -Duser.dir=/path/to/the/files/directory My.class myfile.txt
UPDATE2:
As I wrote in a comment, you can try symlinks. Execute the following commands in the original directory:
ln -s /path/to/the/files/directory datafiles
$JAVA_HOME/bin/java My.class datafiles/myfile.txt
Sorry - ignore. I missed the first line of your question.
You could pass the two paths as an argument to the jar file - then append the path location at runtime. Many ways to do that, here is one:
java -DdirectoryA="/somewhere" -DdirectoryB="/elsewhere" -jar program.jar
and in your code
String pathA = System.getProperty("directoryA");

Java file access

At the moment, in my java program, I am accessing a file that is in the project folder. When I'm loading the file its path is "./src/package/package/file.txt". When I build the program into an executable it dosen't work.
I would prefer the files to be outside of the .jar, but in the same folder, how would I got about this?
Samishal
You can use a relative path if they are in the same folder, such as ./file.txt. That should carry over even with a compiled JAR.
Otherwise, if you're going to be using the same machine and are confident of the placement of the files, you could use an absolute path, however I don't recommend it.
You can use Class.getResourceAsStream to get the InputStream. It works for jar package too. It is not possible to access the file in jar file using File API directly.
InputStream ins = Class.getResourceAsStream("/gigadot/exp/resource.properties");
More details at http://blog.gigadot.net/2010/10/loading-classpath-resources.html
Because the relative path is relative to where your command prompt is when you execute the program, not from where the program lives.
You somehow need to tell the program where to find resources. Most people use a .sh/.bat script to figure out where the script itself lives, and either pass -D flags or set the classpath based on that location.
as a note, I $0 gives you the script as it was run on the command line in linux (it could be relative or absolute), and you can use dirname from there to find it's directory, and alter the java command line.
in windows %~dp0 gives you the directory of the batch script which was run, and you can use that to form your java command line.

Error when executing a JAR file

I've been learning about JAR files and wanted to try and create and run one myself. I carried out the following steps:
Created a project folder with a 'source' subfolder and a 'classes' subfolder
I wrote 2 source files, one with a main method which creates an instance of the other class and runs a simple method in it.
Compiled these to the 'classes' subfolder. I checked to see if they would run. They did
I created a manifest.txt file and filled in the Main-Class: xxxx and hit the return key. I saved this in the sources subfolder
Created a jar file in the classes subfolder by writing
jar -cvmf manifest.txt zzz.jar *.class
Tried to execute the jar file by typing
java -jar zzz.jar
This gives a ClassNotFound exception. If I try to execute the jar by double clicking on it in windows I get an errorbox saying "Could not find the main class xxxx"
I've double checked the spelling of the class inside the manifest file and it's correct.
Possibly important: I have to compile my programs using java -cp . xyz as there is an issue with my classpath. Does this mean that I need to execute jars in a different way as well? I tried
java -cp . -jar zzz.jar
but ended up with the same exception.
Edit: I ended up starting from scratch and now it runs (with the basic -jar zzz.jar command). Frustrating that I don't know what I was doing wrong but glad that it is working!
Shouldn't number 5. be run in the classes subfolder, where all your class files are? And if your classes are in packages, which they should be, you'll likely want to use * instead of *.class..?
To check what your jar file contains you can run:
jar tf zzz.jar
You will probably have to supply the entire path of the .class file you wish to execute after the classpath. ie java -cp xxx.jar classes.mainProgram.class. Where classes is the name of the folder which contains your class files.

Move Jar file to Start Up Folder

How do u move a Jar file to the Start up folder with code? Like within the code either make a Jar file or move it to a different directory with Java.
Edit: So basicly i have a Jar file on the Desktop, I want to move the Jar file to or duplicate the jar file and move that to lets say C:\Program Files (x86) When u run the Jar File
I haven't tested this code, but if I remember correctly you want something along the lines of:
Runtime.getRuntime().exec("cp path/to/jar path/to/destination");
Where the string is the appropriate terminal command for your OS and what you want to do. Different methods surely exist, however I believe this is the easiest way. Here are some example commands:
Copying files:
Windows: cp path\to\jar destination\path
Linux: cp path/to/jar destination/path (note: you may need to prefix this command with sudo if the logged in user doesn't have the proper permissions. This can introduce its own headaches, so tread lightly)
Making a JAR archive:
Windows: jar cf path\to\jar path\to\files
Linux: jar cf path/to/jar path/to/files (again, it is possible you may need the sudo prefix)

Categories