At the moment I am looking for another way to run my Java program from command line, other than adding it to a JAR file. My program has the following number of classes:
The name of the program file - MyProgram
Main class - Server1
second class - Client Handler
Package name - Items
3rd class - User1
4th class - User2
The main class and client handler alongside the package will have to run first in order for user 1 & user 2 to run, because they are client classes and are dependent on the main class.
javac *.java // compliles all java files in the dir
java MyClass // runs the particular file
If one class is dependent on another class that hasn't been compiled yet, the program won't run. So you should compile all files before trying to run the program dependent on other files.
If your files are packaged, then something like this
javac com.mypackage/.*java
java com.mypackage.MyClass
you must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath. Note that the windows classpath separator is a semi-colon ie ;
javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main
Example. Suppose you have the following
Package Named: com.test
Class Name: Hello (Having main)
Java file is located inside "src/com/test/Hello.java"
then, from outside directory:
$ cd src
$ javac -cp . com/test/*.java
$ java -cp . com/test/Hello
Note that you can add -d to specify output directory of your class files whenever compiling
$ javac -d output_directory -cp . com/test/Hello
In windows the same thing will be working too, I already tried
Check out this from Oracle official site
Once you compile your code, you then run this from the top level:
java -cp . com.myprogram.MyProgram
That order thing you describe doesn't matter. They all get compiled together, and MyProgram will reference Server1, etc.
It may be more then you want to tackle right now but you might want to consider a build system like Maven. To start try out; How do I make my first Maven project?
You can use it to predefine the build order and if you want have it create a jar for you (or not).
Sounds like you will just need to open multiple command prompts and compile and run them in the order you need them to run. Let me know if I misunderstood question.
TO EXECUTE TWO JAVA PROGRAMS WHICH DEPENDS TO EACH OTHER.
(for example:two files Complex.java and Solution.java, where Soultion.java depends upon Complex.java.
So Complex.java should be compiled first and then the class file of Complex must be linked with Solution.java and then Solution.class must be executed for Output.)
REFER THE IMAGE WITH SYNTAX.
STEP 1:
COMPILE Complex.java
compiling Complex.java
syntax-
javac -d [path_where_class_File_build] [path_of_the_file\filename.java]
(Solution.java and Complex.java are Linked. ie-Solution.java calls Complex.java)
STEP 2:
COMPILE Solution.java
compiling Solution.java with linking Complex.class
with linking Complex.class(above created in step 1)
syntax-
javac -d [path_where_class_File_build] -cp [path_of_the_first_class_created] [path_of_the_file\filename.java]]
STEP 3:
EXECUTE THE Solution.class
java -cp [path_of_second_class_created] [class_Name]
(created in Step 3)
Related
I run the code in the GitHub project https://github.com/shamsimam/savina
and after creating the jar file with:
java -jar savina-0.0.1-SNAPSHOT-jar-with-dependencies.jar
and try to execute the test I get this message
no main manifest attribute, in Savina-0.0.1-SNAPSHOT-jar-with-dependencies.jar
if anyone can run the test and told me where is the problem.
Savina repo doesn't contain a single file that can run all benchmarks. You have to run each benchmark separately like below
$ java -cp target/savina-0.0.1-SNAPSHOT-jar-with-dependencies.jar edu.rice.habanero.benchmarks.concdict.DictionaryHabaneroRWArrivalOrderBenchmark
$ java -cp target/savina-0.0.1-SNAPSHOT-jar-with-dependencies.jar edu.rice.habanero.benchmarks.facloc. FacilityLocationAkkaActorBenchmark
and so on. You can find the list of all different types of benchmarks on this folder and inside each folder they contain the implementation in each of the actor systems.
Your jar-file doesn't include the manifect, where the class to launch is set, so you can't run it in with java -jar
Instead you should use the full name of the class to run. Here's an example
java -cp my-app.jar com.mycompany.app.App
The code launches a class App.class from the package com.mycompany.app.
I have trying to compile java files at the windows command line using commands such as:
java myProg once I have used javac to create class files.
Problems arise when I use packages with a number of source files.
Often but not always I get main not found errors even though a main exists.
I am not quite sure what some of the directives mean and that is why it seems hit or miss.
Question
what does -cp mean exactly? java -cp src\myDirectory.myfile
sometimes I see:
./ infront of source eg .\src\myDirectory.myfile
on other sites I have found
% javac -cp .;stdlib.jar MyProgram.java
% java -cp .;stdlib.jar MyProgram
while compiling a jar library with java source files
what doesthe ".;" mean?
basically how do I compile three java source java files in one package at the windows command line and what does -cp and .; mean?
-cp means class path if I'm not mistaken.
try reading the following java docs
-classpath path
Specifies the path javac uses to look up classes needed to run javac or being referenced by other classes you are compiling. Overrides the default or the CLASSPATH environment variable if it is set. Directories are separated by semi-colons. It is often useful for the directory containing the source files to be on the class path. You should always include the system classes at the end of the path. For example:
javac -classpath .;C:\users\dac\classes;C:\tools\java\classes ...
https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/tooldocs/win32/javac.html
Answering your question directly, -cp means classpath or path.
Details on commandline arguments used while compiling and running a Java application can be found here: javac - Java programming language compiler
Extracting the description of -cp from that page:
-cp path or -classpath path:
Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.
. means the current directory.
To compile multiple files in a directory use the following:
javac *.java // compliles all java files in the dir
java MyClass // runs the particular file
There are also a bunch of other related questions that should help you resolve this:
How to run a java program from the command line
How do I run java program with multiple classes from cmd?
Problems running a java program from the command line interface
Can't run multiple-class program from command line using packages
I have been testing the examples (HelloWorld.java) from Sphinx4 with Eclipse, but I would like to compile and run them from the command line.
The application needs 5 .jars to be able to run, I have read that in order to compile a java class with multiple .jars I need to execute the following command (as an example I will show short names):
javac -cp one.jar:two.jar:three.jar:four.jar:five.jar HelloWorld.java
The console does not throw any error messages:
parias001#parias001-pc:~/Projects/citadel_voices/sphinx_test > javac -cp jsapi.jar:sphinx4.jar:TIDIGITS_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar:WSJ_8gau_13dCep_8kHz_31mel_200Hz_3500Hz.jar:WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar HelloWorld.java
parias001#parias001-pc:~/Projects/citadel_voices/sphinx_test >
I think that the compilation succeeded. Now I would like to run the application, I read that in order to do this, I have to execute the command as follows (Using short name example as before):
java -cp one.jar:two.jar:three.jar:four.jar:five.jar HelloWorld
This is the message that the console throws:
parias001#parias001-pc:~/Projects/citadel_voices/sphinx_test > java -cp jsapi.jar:sphinx4.jar:TIDIGITS_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar:WSJ_8gau_13dCep_8kHz_31mel_200Hz_3500Hz.jar:WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar HelloWorld
Error: Could not find or load main class HelloWorld
I don't know what is going on here, I should also say that I do not have a lot of experience using external .jars.
The names of the .jars are:
jsapi.jar
sphinx4.jar
TIDIGITS_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar
WSJ_8gau_13dCep_8kHz_31mel_200Hz_3500Hz.jar
WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar
I appreciate any help you can give me.
You have to include current directory in classpath:
java -cp .:one.jar:two.jar:three.jar:four.jar:five.jar HelloWorld
Note the leading .:
From this reference:
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.
I add the path of jdk in the environment variable "path", my position is on the directory of the application: "C:\Users\20900994t\Desktop\applicationArbre_2_1\src\applicationarbre"
I have 4 .java files .
I tried 2 methodes.
The first method were:
1. "javac Main.java" 2. "java Main"
The second method were:
1. "javac *. java" 2. "java Main.java"
These methods generate all of the files . classes of all the classes I have, but at the command line it shows me that the purpose of the other classes are not recognized and when I did "java Main", it shows me "NoClasseDefFoundError"
Actually, those method works well in environment Net Beans, however I need to execute it on command line with .bat file .
Thank's
I suspect you're running it like this:
// In directory applicationarbre
$ java Main
You should be running it like this, in the parent directory:
$ java applicationarbe.Main
You say you've tried several solutions, but you haven't explained what you've tried, or what happened when you tried them, which makes it hard to help you further.
Basically, you need to tell Java the fully-qualified name of the class you want to launch, and that class has to be available on the classpath, which is "the current directory" by default. Anything more than that and you'll need to give us more information.
Not like this:
java applicationarbre/Main
but like this:
java applicationarbre.Main
As for the classpath, maybe in your case this will be enough (if you are in the correct base directory below which are your classfiles):
java -cp . applicationarbre.Main
first compile the class in which main method resides
C:\foldername>javac ClassName.java
then run with statement
C:\foldername>java ClassName
hope it will work :)
Suppose I just created a package "example" and have two classes inside it, "Main" and "Helper".
With the simplest possible compilation (e.g., $javac Main.java Helper.java) I am already able to run it fine as long as I am in the directory containing the example package, by typing this in the command line:
$java example.Main
Questions:
Why would I want to set a CLASSPATH given I can already run the program? I am guessing to be able to type "$java example.Main" from any directory on my machine, but I am not sure.
What happens when I type "java -cp /path/to/your/java/class/file Main" on the command line? Right now I picture there's file containing all the different classpaths, and that command will just add another one to it. Is it the case?
Is there a difference between using "CLASSPATH=/path/to/your/java/class/file" and "java -cp /path/to/your/java/class/file Main" on the command line? How come the second one has the name of the class (i.e. Main) in the end?
Yea, pretty much. That of course assumes you have the path to java in your PATH variable
-cp or -classpath adds it's option (a string) in front of whatever is in your CLASSPATH
Yes, there is a difference. Using CLASSPATH is often more convenient as you tend to set your CLASSPATH once. From then on, java Main is enough to execute the main class. With java -cp /path/to/your/java/class/file Main you have to type the -cp /path/to/your/java/class/file every time.
That being said, both CLASSPATH and -cp or -classpath options usually contain entries pointing to directories containing java libraries used by your program, not the directory of your program itself.