I'm not able to understand certain behavior while using -cp switch with javac. I have two java files in the directory C:\A\B\C> of a Windows 7 machine. The files are Extend.java and TestExtend.java; both belong to the package 'package com.gonni.profile'. I'm getting the following error:
C:\A\B>javac -d . -cp C\Extend.java
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options
C:\A\B>javac -d . -cp 39#$%$fe#%#$%FF#$%GWE C\Extend.java
C:\A\B>javac -d . -cp C\TestExtend.java
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options
C:\A\B>javac -d . -cp 3458$^$%$%BF#W%V#$ C\TestExtend.java
C\TestExtend.java:6: cannot find symbol
symbol : class Extend
location: class com.gonni.profile.TestExtend
Extend exObj = new Extend();
^
C\TestExtend.java:6: cannot find symbol
symbol : class Extend
location: class com.gonni.profile.TestExtend
Extend exObj = new Extend();
^
2 errors
C:\A\B>javac -d . -cp . C\TestExtend.java
C:\A\B>
Extend.java is :
package com.gonni.profile;
class Extend {
class Inner {
}
}
TestExtend.java is :
package com.gonni.profile;
class TestExtend {
public static void main(String[] args) {
Extend exObj = new Extend();
}
}
I am sorry to say it but I do not understand what do you want to do: to compile your program or to make javac to fail?
Path C\TestExtend.java seems wrong. Do you probably mean C:\TestExtend.java?
What is 39#$%$fe#%#$%FF#$%GWE? Do you understand what does -cp mean?
Your classes belong to package com.gonni.profile. It means that they must be under directory com/gonni/profile starting from your source root.
You do not have to supply option -d .. This is a default.
As far as I understand you have several (2 ?) classes without any external dependencies. This means that you do not have to use -cp (that means CLASSPATH) at all.
What to do?
Create directory where your project is. Let's say C:\myproj.
To simplify things for the beginning create directory structure according to your packages. For exampplee if your package is com.gonni.profile you should create directory C:\myproj\com\gonni\profile.
Put your class(es) there.
Open commend prompt and go to C:\proj
Now run command javac com/gonni/profile/*.java
Good luck.
Your source files in this case should be under directory C:\A\B\C\com\gonni\profile - not directy in C:\A\B\C. Option -cp specifies path(s) to look up other compiled classes - not the source files.
Use -sourcepath instead if you want to specify location of source tree:
javac -sourcepath C C/com/gonni/profile/TestExtend.java
Javac requires to list ALL files it must compile in the command line. You cannot just list one and except it to autodiscover others. As a result, large, real world projects are very difficult to build that way. Also, fix a couple of small errors others have already pointed out.
Hence learn Eclipse, NetBeans or the like for IDE-based development or learn Maven, Ant, Make or the like if you want to become a command line master. It is uncommon just to call javac directly at these times.
Related
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)
Probably this is a repeated question, but I just couldn't find an answer to what I am looking for! I am trying to compile and run a java class in a Unix box.
I have the class as:
package tmp.test;
import org.jasypt.registry.AlgorithmRegistry;
class Algo {
public static void main(String[] args) {
System.out.println(AlgorithmRegistry.getAllPBEAlgorithms());
}
}
The files are in the path /tmp/test/. Now I compile the class with the command:
javac -cp jasypt-1.9.3.jar Algo.java
The JAR file is in the same directory. It compiles just fine. But when I run the class file with the command:
java -cp jasypt-1.9.3.jar Algo
I get the error:
Error: Could not find or load main class Algo
I am executing all the commands from the path /tmp/test/.
I tried:
java -cp jasypt-1.9.3.jar tmp.test.Algo
and
java -cp jasypt-1.9.3.jar tmp/test/Algo
Both throw the same error.
I am not sure what I am doing wrong. At first I thought it was the problem of the access thing. So I changed everything using chmod to 777. Everything seems to be fine. Can you please let me know what I am missing here?
I am executing all the commands from the path /tmp/test/
That is the problem. You need to be one level above tmp, not somewhere inside. Then your command line
java -cp /path/to/jasypt-1.9.3.jar tmp.test.Algo
should work. If you insist in starting Java from the subdirectory inside your classpath, you can do this quite contrived thing:
java -cp /path/to/jasypt-1.9.3.jar:../.. tmp.test.Algo
tl;dr
Use the switch -d to compile and then use the fully qualified name of the class to run it.
Compile the class as follows:
javac -d . -cp jasypt-1.9.3.jar Algo.java
The switch, -d specifies where to place generated class files and . stands for the current directory.
Run the class as follows:
java -cp jasypt-1.9.3.jar tmp.test.Algo
Is there an option for javac, say --dry-run, that will instruct the compiler not to do the actual compilation, but to parse the source file(s) and list which .class files (including package path) will be generated?
Consider this example:
$ cat example.java
package whatever.example;
class First {}
class Second {}
$ javac -d . example.java
$ find .
.
./example.java
./whatever
./whatever/example
./whatever/example/First.class
./whatever/example/Second.class
The source file was compiled into two .class files and, as the -d option was specified, package structure was generated. I would like to know such information before compilation. Something like this:
$ javac --dry-run -d . example.java
./whatever/example/First.class
./whatever/example/Second.class
Alternatively, if there is no such an option for javac, is there any third-party utility that can do such a thing?
try
javac -verbose -d . javaclass.java
this actually lists all the actions that a compiler is working on. towards the end you can what all classes have been generated with package structure.
I have compiler via the above code. I get the below output towards the end of the list.
[wrote RegularFileObject[.\com\SC\JustTesting.class]]
[wrote RegularFileObject[.\com\SC\JustTestingSecond.class]]
There are lot of other options, just type javac on the command prompt to look at them.
I don't know if I one can know, prior to compiling, this information.
I'm currently trying to run my first java script:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I decided i'd take a little look into Java. However I come from languages like JavaScript and PHP which don't require any compiling or anything as such.
So far, I think i'm compiling it correctly in the command prompt:
C:\Users\Shawn>"C:\Program Files\Java\jdk1.7.0_25\bin\javac.exe" "HelloWorld.java"
It adds a file called: HelloWorld.class so figured I did something right.
However, now when I try to actually run the program using:
C:\Users\Shawn>"C:\Program Files\Java\jdk1.7.0_25\bin\java.exe" "C:\Users\Shawn\HelloWorld.class"
I get this, Error: Could not find or load main class C:\Users\Shawn\HelloWorld.class.
However, if I try that same command but use javac.exe instead I get:
javac: invalid flag: C:\Users\Shawn\HelloWorld.class
Usage: javac <options> <source files>
use -help for a list of possible options
Why is this happening? Why isn't my program executing correctly?
The java command takes the name of the class, not the name of the file.
It then uses the Java class loader to find the .class file for that class in the current directory or the class path.
When you pass HelloWorld.class, it looks for a class named class in the package HelloWorld.
(that would be ./HelloWorld/class.class)
You need to pass HelloWorld.
As others have pointed out the argument needs to be just the class name, without the extension .class. There is a second requirement that must be met, though: the class file must be in the classpath.
It's usually not advisable (although convenient) to include the current directory in the global classpath, but you can override it on the command line:
java -cp . HelloWorld
You can also specify an explicit path:
java -cp "C:\Users\Shawn" HelloWorld
If your Java program uses other classes, include the global classpath like this:
java -cp "%CLASSPATH%;." HelloWorld
See the following javac documentation for more info. especially the section on Cross-Compilation Options.
I have been sitting on it for a while and can't figure it out, although I think it's quite easy...
I have to compile the following program using javac (the program has one class and one testing class):
a class is in folder ./src/cplx/
a testing class is in folder ./test/cplx/
junit lib is in ./lib
and:
classes should be built to ./build/slasses
testing classes should be build to ./build/test
Please help me with writing a proper javac command to compile the code.
I used the suggested command end het the following error, it looks like test class doesn't see the class i have built?
amaltea:testowanie/zad1% javac -d ./build/classes ./src/cplx/*.java
amaltea:testowanie/zad1% javac -classpath ./lib/junit-4.8.2.jar -d ./build/test ./test/cplx/*.java
./test/cplx/ComplexTest.java:20: cannot find symbol
symbol : class Complex
location: class cplx.ComplexTest
Complex a = new Complex(1.1, 2.2);
^
./test/cplx/ComplexTest.java:20: cannot find symbol
symbol : class Complex
location: class cplx.ComplexTest
Complex a = new Complex(1.1, 2.2);
^
2 errors
amaltea:testowanie/zad1%
You can specify only one root destination directory with javac. If you want the root itself to be different you need to compile them separately.
javac -d ./build/classes ./src/cplx/*.java
javac -classpath ./lib/junit.jar -d ./build/test ./test/cplx/*.java
Although it's nice to start off using javac to grasp what is going on at a lower level and understand the language and tools before you begin using more advanced stuff, I think some Ant build script or an IDE like Eclipse or NetBeans would serve you better. At least if you just want a result rather than understanding all the details. You can always learn more about those later. Anyway, the official documentation should tell you what you need to know: http://download.oracle.com/javase/6/docs/technotes/tools/windows/javac.html
You have to specify one more thing in -classpath option..
it should be:
javac -classpath ./build/classes/:lib/junit-4.8.2.jar -d ./test/classes test/cplx/*.java
That's information where your Complex.class file is.