Move Jar file to Start Up Folder - java

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)

Related

How do I install and set up Java speech (jsapi) for my iMac running the latest OS Sierra?

I am relatively new to Apple OS and thus am not able to figure out as to how do I download, install then set up Java speech jar files and set up the classpath.
I tried everything possible from
Downloading FreeTTS,jsapi and trying to add the jsapi.jar to /library/extensions folder
Trying to use chmod command
Trying to open the jar file (which returned an error and asked me to check console)
Tutorials asking me to drop jar into the "lib" folder (I can't really figure out what and where is the "lib" folder in mac)
Basically I want to use all the capabilities of javax.speech in my Java programs.
JSAPI is pretty much abandoned, you won't be able to get lot from it. If you want text-to-speech use OpenMary directly without JSAPI, it provides a good selection of modern voices.
Download FreeTTS. Extract. Open the extracted /lib folder. chmod +x ./jsapi.sh and afterwards sh ./jsapi. Read the BCEL and accept.
Accept (y/n)?:
y
sed: --print-text-domain-dir: No such file or directory
x - creating lock directory
x - extracting jsapi.jar (binary)
As you can see there is the jsapi.jarbeing extracted into the lib folder (which was not there before). Now you can add the lib folder (it says it will be enough to point to the lib/freetts.jar) to your class path of any application that is using FreeTTS.
You could add it into some directory in your userspace folder and add to your ~/.bash_profile the line export JS_API_HOME=~/the/path/lib where ~/the/path would be where you stored the extracted archive. Then, you have to add the environment variable $JS_API_HOME to every build/classpath where you want to use the library.
For example, java -cp $JS_API_HOME -jar moep.jar

jar not reading input file directory recursively

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

Can I make a jar file runnable from any directory?

I am curious if I can take a .jar file and somehow add it to my classpath so that I can run it from any directory. For example, let's say I have a .jar located at /home/setup/someJar.jar. Is there a way I can run this from another directory (preferably any) so that I do not have to navigate back to /home/setup/ whenever I want to run it?
I tried adding the path to my .bash_profile file by adding :/home/setup to the PATH= line, but didn't work.
I think you probably want an 'alias'.
http://www.linfo.org/alias.html
alias myJarShortcut="java -jar /direct/path/to/nameOfYourJar.jar"
Add this to your .bash_profile and it will be available every time you boot up.
It's possible but not so easy. you have 3 possiblities
Make an alias or symbolic link (create symbolic link)
Start it as deamon
start it as service (create service)
after ready the information above
you can start it like this
service [yourservice] start|stop|restart
You could create a bash script that execute your .jar file in the directory of your choice, with of course the right path to your .jar file.

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.

Categories