I want to compile a file inside a folder with respect to the current path I have opened the command prompt. But I used it like this
javac -d A.java
But this was a possible effort in ubuntu. But it is not working in windows. Can anybody help to make this work in windows7..
You need to give the path, where you want to store your classes with package structure. May be a . for current working directory. Try like this:
javac -d . A.java
Related
I have 4 Java-Data:
Common.java
Constants.java
KeywordsEditor.java
ExecutionEngine.java (There is here a Main-Method)
I have successful compiled in Command-Line with this Command from Project-Directory (C:\ProjectDemo\src\main\java\ValueInput)
javac -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" *.java
I got 4 Data .class in the same Directory. Now i want to run them with this code:
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" ExecutionEngine
But i got Error:
Error: ExecutionEngine main class could not be found or loaded
```
I've tried with some same code else:
```
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;libs\*;selenium-server-standalone-3.141.59.jar" ExecutionEngine
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;libs/*;selenium-server-standalone-3.141.59.jar" ExecutionEngine
```
And some more, but they don't work. Can somebody help me?
Update
From your comment, I learnt that you have package ValueInput; mentioned in ExecutionEngine.java. Therefore, you should use the switch -d when compiling:
javac -d . -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" *.java
The option -d . asks the compiler to place the generated class files at the current directory. Now, if you use the command ls in Mac/Unix or dir in Windows, you will see a directory, ValueInput has been created and all the .class files have been placed inside this directory. Learn more about the switches by simply using the command javac
In order to execute ExecutionEngine.class, you can now use the following command:
java -cp ".;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" ValueInput.ExecutionEngine
You can also check this answer for a similar solution.
Side note: You should follow the Java naming conventions. As per the convention, the name of the package should be something like value.input.
Original answer
The root cause of the problem is using only jars with -cp. You missed realizing that your ExecutionEngine.class is not in the jars; rather it is at the current directory which is denoted by a dot (.) which you missed to include in the classpath.
Thus, the correct command will be:
java -cp ".;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" ExecutionEngine
It doesn't matter where you put . i.e. the current directory e.g. the following will also work for you:
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar;." ExecutionEngine
Note for Mac:
The separator used for this purpose in Mac is : instead of ; e.g.
javac -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
java -cp mysql-connector-java-5.1.49.jar:. MysqlDemo
Note for Java-11 onwards:
Java-11 allows launching Single-File Source-Code programs without compiling e.g.
java -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
You can learn more about it from this article.
I tried
javac Something.java -cp ".; some\\directory\\xxx.jar"
and it worked. I could compile the file in this way.
But when running the file, i realized that I must put -cp in front, making it
java -cp ".;some\\directory\\xxx.jar" Something
Why is there such a difference?
Thanks
while running, anything you provide after Something will be treated as program argument
I haven't been able to find a good answer to this yet, but when using javac, I find that for classpath I can't seem to get it to work using a relative path to the current working directory, although it seems to work for the target directory and source file location. Here is what we're currently using:
javac java/src/com/<our-company>/ecommerce/<vendor-tool>/executor/*.java java/src/com/<our-company>/ecommerce/<vendor-tool>/util/*.java -d ./target/java/bin -cp /c/Git/<our-api>/java/lib/some.jar:/c/Git/<our-api>/java/lib/another.jar -verbose
And we would like to use something like this:
javac java/src/com/<our-company>/ecommerce/<vendor-tool>/executor/*.java java/src/com/<our-company>/ecommerce/<vendor-tool>/util/*.java -d ./target/java/bin -cp java/lib/some.jar:java/lib/another.jar -verbose
or perhaps:
javac java/src/com/<our-company>/ecommerce/<vendor-tool>/executor/*.java java/src/com/<our-company>/ecommerce/<vendor-tool>/util/*.java -d ./target/java/bin -cp ./java/lib/some.jar:./java/lib/another.jar -verbose
The first example seems to work, but the other two get an error that the packages within those jars cannot be found. Since we are using Git, I would prefer the whole build path be relative to allow for wherever the individual's Git repo is located locally--as well as be flexible concerning which operating system they may be using (this is being run in a shell which should operate the same for each of the platforms).
I'm trying to compile some java code for hadoop and need to know what classpath I need to specify. For cloudera I use this below but what do I use for a MapR installation? Surprisingly I could only find how to set the classpath in google, not what to set it to.
javac -classpath "/opt/cloudera/parcels/CDH-4.6.0-1.cdh4.6.0.p0.26/lib/hadoop/client/*" mr.java -d mr
Found the answer by trial and error. Oddly google is very silent on this and all the books and examples I've read appear to assume this is too obvious to bother printing.
mkdir MyClass
javac -classpath "/opt/mapr/hadoop/hadoop-0.20.2/lib/*" MyClass.java -d MyClass
jar -cvf MyClass.jar -C MyClass .
Additionally, if you want the hive libraries, eg for compiling a hive UDF:
javac -classpath "/opt/mapr/hadoop/hadoop-0.20.2/lib/*:/opt/mapr/hive/hive-0.12/lib/*" MyClass.java -d MyClass
EDIT: one thing I would add is make sure you put quotes around the path, otherwise linux expands it on the command line which is not what you want. The * in the path needs to be passed to java as is.
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)