How would i execute this command in java? [duplicate] - java

This question already has an answer here:
How do i run a UNIX terminal from Java and send commands to it?
(1 answer)
Closed 9 years ago.
How would i make a java program find and delete all the files in my computer that have the extension: .jar?
I have the command:
find . -type f -name "*.jar" -exec rm -i {} \;
I know how to execute commands, but i don't know how to tell it to execute this command.

Simply, you CAN't delete ALL .jar files on computer. Because your java program needs JVM to run and since jvm uses jar files present under lib of JRE insallation. And in order to run your program you need lib jar files.
Although, you can give it a try with example from mkyong.
http://www.mkyong.com/java/how-to-find-files-with-certain-extension-only/

You can use a FilenameFilter to see if files match your pattern. http://docs.oracle.com/javase/7/docs/api/java/io/FilenameFilter.html
Once you have found a file you want to delete, use File.delete() to actually delete it.
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html#delete%28%29

Related

PATH variable not been read from ~/.bash_profile [duplicate]

This question already has answers here:
How to invoke a Linux shell command from Java
(3 answers)
Closed 6 years ago.
I'm using RedHat Linux bash command prompt. My requirement is to run a command using following code snippet where command could be anything which is allowd on command prompt eg: javac etc.
Process p = Runtime.getRuntime().exec(command);
Now i have set my commands bin directory in PATH variable by editing ~/.bash_profile file
export PATH=$PATH:<my commands bin directory>
it runs perfectly when i manually run the command by opening a new command prompt, but when i'm trying to run it using process command it fails as the PATH variable does not have my commands bin directory.
It seems when "Runtime.getRuntime().exec(command);" invokes a new bash shell it does not include or read ~/.bash_profile file.
I have also verified that the user is same when running manually and using java code.
Could anyone point out whats issue here?
Runtime.getRuntime().exec() doesn't search PATH by default. You will either need to find a different method that does this for you or implement it yourself by loading PATH, possibly parsing it, and then iterating through it to find the executable.

Something like environment variable in Ubuntu [duplicate]

This question already has answers here:
How to install Java application to my linux system
(2 answers)
Closed 6 years ago.
I'm new to Linux and Ubuntu environment.
I'm calling a jar file like this :
java -jar app.jar /somearg /anotherarg
But I find this ugly and I want to call my jar file like :
MyApp /somearg /anotherarg
So I think I have to set a environment variable like MyApp = java -jar app.jar.
But I don't know how can I do it.If anyone can help me I'll be very pleased.Thanks.
Using
java -jar app.jar /somearg /anotherarg
instead of
myapp /somearg /anotherarg
saves you only 2 words, so not that much. Anyway if you run your application frequently it is a good idea to provide an alias. If you are using bash shell (echo $SHELL shows something like /bin/bash) then here's the command you can paste in your terminal:
alias myapp="java -jar /path/to/your/app/app.jar"
After that you can use
myapp /somearg /anotherarg
It is important to provide the whole path to your app.jar file if you run it from different locations. Also if you want that your alias is permament (i.e. it is always after you log in) just add the same line at the end of ~/.bashrc file, using for example:
pico ~/.bashrc
PS. You don't need slash symbol to provide parameters. In unix systems it is more common to use "-" to set argument options.
PSS. Unix systems are case sensitive and it is typical that command line programs are all written in lowercase.

Running java programs from the command line on Windows 10 [duplicate]

This question already has answers here:
javac is not recognized as an internal or external command, operable program or batch file [closed]
(6 answers)
Closed 6 years ago.
I'm trying to run a basic "Hello World" app from the command line on windows 10 using
javac MyFirstProgram.java
but i receive
'javac' is not recognised as an internal or external command, operable program or batch
naturally, the first thing i did was google the problem and many solutions were presented, tried a few but nothing worked, has anybody else experienced this?
Steps to fix this error in windows 10/8/7
1.Check your javac path on Windows using Windows Explorer C:\Program Files\Java\jdk1.7.0_02\bin and copy the address.
2.Go to Control Panel. Environment Variables and Insert the address at the beginning of var. Path followed by semicolon. i.e C:\Program Files\Java\jdk1.7.0_02\bin; . Do not delete the path existent, just click in and go to the left end and paste the line above. Do not try anything else, because you just need to link your code to "javac.exe" and you just need to locate it.
3.Close your command prompt and reopen it,and write the code for compile and execution.
You need to add the location of your JDK to your PATH variable, if you wish to call javac.exe without the path.
set PATH=%PATH%;C:\path\to\your\JDK\bin\dir
Then...
javac.exe MyFirstProgram.java
OR, you can simply call it via the full path to javac.exe from your JDK installation e.g.
C:\path\to\your\JDK\bin\javac.exe MyFirstProgram.java

Makefile to compile Java with external library [duplicate]

This question already has an answer here:
how to make makefile for java with external jar file
(1 answer)
Closed 8 years ago.
I want to write a Makefile that compiles and runs the following code. My question here is how to add an external library using Make syntax?
javac -cp commons-cli-1.2.jar Iperfer.java
java -cp .:commons-cli-1.2.jar Iperfer -c -h localhost -p 1234 -t 12
My question here is how to add an external library using Make syntax?
The Answer is in the question. You would use the same command as you would from the command line ...
This is really a Java agnostic question. It is really about how to write a Makefile. But presumably if your teachers want you to use make to build Java, they will have provided you with a sample / template Makefile to get you started. If you showed us that template, we could tell you how best to modify it.
But assuming that you don't have a specific template in mind, then this a duplicate of an existing Question.

How to run a Java program on every file in a directory in UNIX?

I have a java class file which I need to run on every file in a directory. Its in the form of
java StripEnronHeaders < FILE NAME >
I'm very new to UNIX so I'm wondering if there is a UNIX command or set of commands that will allow me to run that java program recursively for every file in a directory?
find . -type f -exec java StripEnronHeaders {} \;
though this will execute java for EVERY file you find. If that app can accept multiple files on the command line, then this'll be more efficient:
find . -type f|xargs java StripEnronHeaders

Categories

Resources