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.
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)
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.
If I have all my .java and .class files in one place (i.e. in the default package) then everything is OK and I do all the JNI stuff, etc.
But in this case I have package-ception (lots of directories), my class and Java files are separated in /bin and /src and so on. And I need to generate the header file, but I am getting errors all the time. I tried so many commands, I saw different tutorials. I am already out of options.
So my project is in c://gvk/SEP3 and then the class and Java files with the native methods that I am gonna use are in /bin/CalculatorServer and /src/CalculatorServer
I have all the time run the javah command from the directory where the class file with the native methods is. The commands I tried so far are:
javah -d ./CalculatorServer NativeMethodsCalculator
Error: Could not find class file for 'NativeMethodsCalculator'.
javah -d ./CalculatorServer CalculatorServer.NativeMethodsCalculator
Error: Could not find class file for 'CalculatorServer.NativeMethodsCalculator'.
javah -d c://gvk/SEP3/bin/CalculatorServer -classpath c://gvk/SEP3/bin/CalculatorServer NativeMethodsCalculator
Error: Could not find class file for 'NativeMethodsCalculator'.
javah -classpath c://gvk/SEP3/bin/CalculatorServer -o NativeMethodsCalc.h src.CalculatorServer.NativeMethodsCalculator
Error: Could not find class file for 'src.CalculatorServer.NativeMethodsCalculator'.
javah -jni bin.CalculatorServer.NativeMethodsCalculator
Error: Could not find class file for 'bin.CalculatorServer.NativeMethodsCalculator'.
What you didn't try: go just to /bin/ (not into CalculatorServer) and run
javah -jni CalculatorServer.NativeMethodsCalculator
This is the only way how to run it. Just look at the javah doc. It says "fully-qualified-classname" in the synopsis. "Fully qualified" means full classpath. You were giving it only the classname. It worked for you so far only because you were using a default package, which means that your fully qualified classname was equal to a bare classname.
Option -d and -o doesn't influence the class lookup, only the storage of native result. All the variants you tried do not make any difference to your mistake.
I have all the time run the javah command from the directory where the class file with the native methods is
That's your mistake. You should run it from the directory that contains the outermost package, with the inner packages and their .class files below it. Then you don't need a -d argument or a -classpath argument. Assuming your outermost package is CalculatorServer, you should be in the directory containing CalculatorServer, and the command line required is javah CalculatorServer.NativeMethodsCalculator.
In the following scenario:
APP_HOME=/Users/me/Documents/workspace/Mimer/bin
javac -cp $APP_HOME/lib/*.jar:: BCClient.java
Assuming $APP_HOME/lib contains all the jars needed
What would cause the following:
BCClient.java:35: package com.thoughtworks.xstream does not exist
..
It looks like files libraries needed by the .java are not found, except that when i do the following code fails with the same error
javac -cp "$APP_HOME/lib/xstream-1.2.1.jar;$APP_HOME/lib/xpp3_min-1.1.3.4.O.jar" BCClient.java
This should work with a compiler Java 6+. But if you execute this command from a shell that perform wildcard expansion, then you need to put the wildcards in quotes. More details can be found here.
/Library/Java/Home/bin/javac -cp "$APP_HOME/lib/*.jar:." BCClient.java
I have a package called studentServer which contains two sub packages student and common.
The common folder has references to the student package and i would like to be able to compile this. How could i do this?
javac student\*.java - compiles the student package
but when i try something similar with the common package errors are thrown - I understand it's something to do with the classpath
javac -verbose -classpath "\student" common\*.java
But I couldn't get this working. Any help would be great.
http://pastebin.com/m2a2f5d5d - here's the output from the compiler
This is a bit vague, but I suspect the classpath for the student code is wrong. Try without the leading backslash.
If you have a directory structure
source/
studentServer/
student/
common/
classes/
And you're in the directory above source, then you want to set the source path to 'source' with the -sourcepath option. You probably also want to use the -d option to tell javac where to put the compiled classes, so they aren't all mixed up with the source:
java -d classes -sourcepath source source/studentServer/student/*.java source/studentServer/common/*.java
go like this
c:\>
use change directory command cd until you get the desired directory
(ex: c:\javaEx\proj1\)
now
cd javaEx go like this
c:\javaEx\proj1\javac *.java
now compilation done in all java files in the proj1 directory.