Setting the classpath - shell

hi i have written one shell script which is performing 1 task.
java -jar abc.jar $* -adminid $j_username
before this command i want to set classpath(or want to refer) of all jars which are in particular lib folder, how to do that?

set CLASSPATH=pathtojars1;pathtojars2
before your java command.
Or:
java -classpath

One way to do it would be:
set CP=abc.jar:someother.jar
java -cp $CP your.main.Class $* -adminid $j_username
It is worth while to note that when using -jar you can't specify other JARs/resources on the classpath i.e. -cp switch is ignored hence you would have to choose between the two.
Create a standalone JAR which
incorporates other JARs/resources
Have different JAR files but instead
of using -jar specify the main
class when running the program

Related

How can I run testng.xml file in linux with cmd

How can I run testng.xml file in linux with cmd ?
What is command to run in linux command
java -cp "path to the testng jar" org.testng.TestNG testng.xml
For testng you need only one jar.So put that in the classpath and for the compiler to understand that it was a testng xml file you have to add "org.testng.TestNG" and run like a normal java program.
The testNG jar file can found in this link if you didn't have the jar file
-> You have to remember one thing,The compiled class file must be in the same directory you are running from the test suite xml
cd <Location of TestNGProject> ie. D:\workspace2\TestNG java -cp
D:\workspace2\TestNG\lib\*;D:\workspace2\TestNG\bin org.testng.TestNG testng.xml
java -cp argument is used to provide path to classes and libraries that our program need to run

Spring boot running a fully executable JAR and specify -D properties

The Spring Boot Maven and Gradle plugins can now generate full executable archives for Linux/Unix operating systems.Running a fully executable JAR is as easy as typing:
$ ./myapp.jar
My question is in this case how to set -D properties, e.g.
-Dspring.profiles.active=test
In addition, if server does not install jdk , could this fully executable jar still run?
There are two ways to configure properties like that:
1:
By specifying them in a separate configuration file. Spring Boot will look for a file named like JARfilename.conf which should be stored in the same folder like the JAR file. There you can add the environment variable JAVA_OPTS:
JAVA_OPTS="-Dpropertykey=propvalue"
2:
Or you can just specify the value for the environment variable in the shell before you execute the application:
JAVA_OPTS="-Dpropertykey=propvalue" ./myapp.jar
Have a look at the documentation for the complete list of available variables: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#deployment-service
Regarding your second question: To execute a JAR, you don't need a JDK, a JRE is sufficient (but you need at least that, if you don't have any java installed on the server, the application won't run).
By default SpringApplication will convert any command line option arguments (starting with ‘--’, e.g. --server.port=9000) to a property and add it to the Spring Environment. As mentioned above, command line properties always take precedence over other property sources.
e.g.
$ java -jar myapp.jar --spring.application.json='{"foo":"bar"}'
please see http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/

How do I run a java program in cmd?

I have created a program in Java, but it is not taking the inputs correctly in edit-plus(compiler) so now I want it to run in cmd. My JSK file is at : C:\Program Files\Java\jdk1.7.0_21\bin and my Java file is at: C:\TurboC4\TC\java new programs
Please tell me the steps to run it in cmd.
On the command line use:
java -jar path/to/your/jar_file.jar
if you do not have a jar file, than you have to compile first your Java classes:
javac -g Foo.java
if you have just a single file (containing a static void main()) than you can simply run it with:
java path/to/your/compiled_class_file [<command line args>, ...]
Note: Run the command above without .class extension. i.e.
java Foo
if you want to generate a jar file from your compiled .class files run:
jar cf jar-file input-file(s)
However, I would recommend you to use a IDE that compiles, packs and runs your code for you automatically with one click. i.e. IntelliJ or Eclipse
If your class is not into a package and is compiled as java.class in C:\TurboC4\TC\:
cd C:\TurboC4\TC\
C:\Program Files\Java\jdk1.7.0_21\bin java
If you just want to run your single java class as a command line program, then the answer is in This question.
Example:
java my.class.HelloWorld
If you have compiled an entire java project into a JAR file, check this Stackoverflow question for the answer.
Example:
java -cp c:\location_of_jar\myjar.jar com.mypackage.myClass

How to Execute Clojure File?

How can I run a clojure file/script, which uses clojure-contrib, from the command line? My (winodws) machine has clojure-1.2.1.jar and clojure-contrib-1.2.0.jar on the classpath. Note: I can start the REPL with: java clojure.mainand I can execute *.clj files that DO NOT use clojure-contrib with: java clojure.main file-name.clj
You need to add the clojure-contrib jar to the classpath. Since you are using windows, you add multiple classpaths by separating the entries with semicolons.
java -cp clojure-1.2.1.jar;clojure-contrib-1.2.0.jar clojure.main file-name.clj
The above code should enable you to run your file-name.clj script which depends on clojure-contrib.
This line will run a clojure script "hello-world.clj". It first adds the clojure jar to the class path first and then will execute the file.
java -cp clojure.jar clojure.main hello-world.clj
for those used to text editor + lots of shell work
lein run isn't bad
and lein projects can help you organize deps and other proj-specific

How to run Clojure tests on Windows?

I put Clojure in C:\clojure-1.1.0, and start the REPL by:
java -cp clojure.jar clojure.main
In \test\clojure\test_clojure, there are a bunch of test files.
How to run these?
For example, I tried:
java -cp ......\clojure.jar clojure.main data_structures.clj
And it didn't work.
Two choices:
Once the REPL starts you can manually load the file with:
(load-file "\data_structures.clj")
You can pass a command-line option telling it to load/eval a file upon boot:
java -cp clojure.jar clojure.main -i \data_structures.clj -e "(do-stuff)"
We use (clojure 1.2):
java -cp "./target/***-0.1-jar-with-dependencies.jar;src/clojure" jline.ConsoleRunner clojure.main run.clj
I think with tests you can use similar way.

Resources