Simple clarification on javac command - java

I am studying Ant scripts in Java reading this Hello World tutorial: http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html
In the previous tutorial it create a new directory by DOS md src command (mkdir in Linux)
then put the following simple code into: src\oata\HelloWorld.java:
package oata;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Now it compile it by this shell statment:
md build\classes
javac -sourcepath src -d build\classes src\oata\HelloWorld.java
java -cp build\classes oata.HelloWorld
I know that javac compile the classess but what exactly do this line?
javac -sourcepath src -d build\classes src\oata\HelloWorld.java
I think that I am sayng to javac that src is where are the sources class to compile, then the -d say that build\classes i the path where to put the compiled class
But what it means the final: src\oata\HelloWorld.java?
Tnx
Andrea

It means the filename(s) to compile.
The purpose of the -sourcepath option is to tell the compiler where the source files for dependent classes may be found. It doesn't imply that everything in that directory should be compiled.

Related

How to compile and run a runnable JAR using only javac and jar

How can I compile and run a runnable JAR file using only javac and jar? I don't want to build the JAR file with an IDE or with tools like Gradle, Maven, or Ant, because I want to understand, for my own edification, how I can compile a runnable JAR using only javac and jar, and then how to correctly run this JAR with java.
I have tried building the JAR like this:
$ javac Example.java
$ file Example.class
Example.class: compiled Java class data, version 55.0
$ jar cvf Example.jar Example.class
added manifest
adding: Example.class(in = 682) (out= 456)(deflated 33%)
$ file Example.jar
Example.jar: Java archive data (JAR)
Then I tried various ways to execute the JAR, but none of them worked:
$ java -jar Example.jar main
no main manifest attribute, in Example.jar
$ java -cp '.;Example.jar' main
Error: Could not find or load main class main
Caused by: java.lang.ClassNotFoundException: main
$ java -cp '.;Example.jar' Example
Error: Could not find or load main class Example
Caused by: java.lang.ClassNotFoundException: Example
$ java -cp '.;Example.jar' -jar Example.jar Example
no main manifest attribute, in Example.jar
$ java -cp '.;Example.jar' -jar Example.jar main
no main manifest attribute, in Example.jar
My code, just to show I have an Example class and a main() function:
import java.util.ArrayList;
import java.util.List;
public class Example {
private static class A {
public int x = 7;
}
public static void main(String[] args) {
List<A> list = new ArrayList<>();
A a = new A();
list.add(a);
a.x = 5;
System.out.println(a.x);
System.out.println(list.get(0).x);
}
}
My javac, jar, and java tools are all version 11.0.15.
I tried looking at the answers on Compile and run with javac and java using classpath classes in jar and javac compiles files and jars but java fails, but none of the suggestions in the answers to these two questions worked for me.
goose#t410:/tmp$ javac Example.java
goose#t410:/tmp$ jar cvfe example.jar Example Example*.class
added manifest
adding: Example$A.class(in = 293) (out= 233)(deflated 20%)
adding: Example.class(in = 682) (out= 457)(deflated 32%)
goose#t410:/tmp$ java -jar example.jar
5
5
Note the penultimate argument to jar is the name of the main class
Thanks to #f1sh and #pdem, I found an answer:
$ javac Example.java
$ jar cfe Example.jar Example Example.class 'Example$A.class'
$ java -jar Example.jar
5
5
Is #f1sh points out in there comment, I needed to (A) include the Example$A.class class when creating the JAR, and (B) run the java command with the correct arguments.
As for making the JAR executable, a manifest file is required. A manifest file can be created manually, or you can let jar create it for you with the e option. The general format for using the e option is
java cfe <name of jar> <name of main class> <list of .class files>
For more info on using the e option or writing a manifest file by hand, see Setting an Application's Entry Point or one of the other child pages of Working with Manifest Files: The Basics.

The command "javac -h" and "javah -jni" can't find the file I specified

I'm trying to generate the header file (MyClass.h) using the command javah -jni MyClass.java since I use the JDK 1.8 but I also tried with the other version of the command javac -h MyClass.java but I always get the following result in my terminal :
All my .java or .class file are in this folder.
Thank you for the help
[edit] I also tried with the .class file but the output is the same with javac -h but with javah -jni I get the following output :
Try to compile SimpleJNI.java first and then execute javah command.
Sample class
public class SimpleJNI
{
public static void someMethod()
{
System.out.println("Some method");
}
}
Compile is using javac with -classpath if there is any external dependency required.
javac -classpath <path_to_dependency> SimpleJNI.java
or without -classpath if there is no dependency
javac SimpleJNI.java
then execute javah with your .class file
javah SimpleJNI

How to use java -cp and -jar in a togther

I am trying the following:
java -cp <path to the additional required jar > -jar <jarname>.jar
I am still getting a java.lang.NoClassDefFoundError when running the above command.
Looks like it still cannot find the external jar mentioned after -cp.
Is that the correct syntax while giving the java command?
You can't use the -jar and -classpath options together. If you want to use the -jar option you need to add the second JAR file to the Class-path attribute in the manifest of the first JAR file.
You could add the additional jar to the bootclasspath
java -Xbootclasspath/a:additional_required.jar -jar main.jar
example
Foo.java
package foo;
public class Foo {
public static void main(String[] args) {
new bar.Bar();
}
}
Bar.java
package bar;
public class Bar {
public Bar() {
System.out.println("foobar");
}
}
manifest.mf
Main-Class: foo.Foo
Execute the commands
javac -d . Bar.java Foo.java
jar cf Bar.jar bar/
jar cmf manifest.mf Foo.jar foo/
java -Xbootclasspath/a:Bar.jar -jar Foo.jar
output
foobar
Given the following command you provide:
java -cp <path to the additional required jar > -jar <jarname>.jar
it will fail because the 'classpath' value must be a ';' separated value. So try adding a ; after your classpath values. For example:
java -cp A.jar;Bjar; -jar <jarname>.jar
and it is mandatory even if you have only one jar file in you classpath string:
java -cp A.jar; -jar <jarname>.jar
Good Luck.

compile sample.java and jgraph_5.8.3.1.jar

In my java application I need to compile my own java file and jgraph.jar file simultaneously ...how can I Compile both file at the same time in command prompt....?
Try following:
c:\ javac -cp "x;y" MyClass.java
where x= path_to_folder_That_contain_your_java_file
and y=path_to_jar
Example:- If your java file resides under c:\JD foler and your jar file is under c:\Jar then do as follows:
c:\ javac -cp "c:\JD;C:\jar\jgrapgh.jar" MyClass.java
Edit:-
If you have package statement in your class then give folder path upto start of that package. For example if you have "example" package and it resides under C:\ than try this:
C:\> javac -cp "c:\;c:\example\jgraph.jar" first.java
Hope this helps. For any other help feel free to comment on this answer.

Compiling multiple packages using the command line in Java

Hi i have been using an IDE but now I need to run and compile from the command line.
The problem is that I have multiple packages and I have tried to find the answer but nothing has worked.
So I have
src/
Support/ (.java files)
Me/ (.java files)
Wrapers/ (.java files)
Do you know how to compile everything with javac?
This should do it (may require additional classpath elements via the -cp command line switch):
javac Support/*.java Me/*.java Wrapers/*.java
But if your build process gets more complex (and it will!), you should look into using Apache Ant for build automation.
You should use build tools like Maven or Ant for such tasks.
In the initial stages, when the project is not very complex you can use the following line to compile, with appropriate classpath in place(as suggested by #Michael):
javac Support/*.java Me/*.java Wrapers/*.java
javac -d compiled $(find src -name *.java)
If you really need to just use javac and standard UNIX commands you could to this:
find src -name \*.java -print0 | xargs -0 javac -d classes
The real answer is javac -d (places where classes to be built and placed) -sourcepath (source of the package at the root) -cp (classpath of the dependencies which can again be classes folder where the classes are build and kept) full qualified name of the java file.
Ex javac -d classes -sourcepath src -cp classes src\com\test\FirstSample.java
The FirstSample.java contains the main method. Pacjage structure mentioned below.
Before compiling
HomeApp
--src
------com\test\FirstSample.java (First Sample using the FirstPojo.java)
------com\test\FirstPojo.java
--classes
After compiling
HomeApp
--src
------com\test\FirstSample.java (FirstSample.java using the FirstPojo.java)
------com\test\FirstPojo.java
--classes
------com\test\FirstSample.class (FirstSample.class using the FirstPojo.class)
------com\test\FirstPojo.class
In many cases Ant is overkill. Just use a BAT file if you are in windows or a shell script (sh file) if you are in linux. You can create a text file which includes all your javac commands and just run that file when you want to build.
For example, I use the following bat file to build one of my apps:
#echo off
echo Building Shazaam...
del classes\com\aepryus\shazaam\*.* /q
del classes\com\aepryus\shazaam\engine\*.* /q
del classes\com\aepryus\shazaam\domain\*.* /q
del classes\com\aepryus\shazaam\persist\*.* /q
del classes\com\aepryus\shazaam\view\*.* /q
del classes\com\aepryus\shazaam\task\*.* /q
del classes\com\aepryus\shazaam\action\*.* /q
del classes\com\aepryus\shazaam\controller\*.* /q
javac src\com\aepryus\shazaam\*.java -classpath \lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepLoom.jar; -d classes
javac src\com\aepryus\shazaam\engine\*.java -classpath \lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepLoom.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\domain\*.java -classpath \lib\AepUtil.jar;\lib\AepLoom.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\persist\*.java -classpath \lib\AepUtil.jar;\lib\AepLoom.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\view\*.java -classpath \lib\Servlet.jar;\lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\task\*.java -classpath \lib\AepUtil.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\action\*.java -classpath \lib\Servlet.jar;\lib\AepUtil.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\controller\*.java -classpath \lib\Servlet.jar;\lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepRPC.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
cd classes
jar cf ..\war\WEB-INF\lib\Shazaam.jar .
cd..
echo Complete
To compile Run below command [it will store all class files in classes folder]
javac -d classes Support/*.java Me/*.java Wrapers/*.java
**Note : classes folder should be created first
To Run java application, run below command
java -cp classes {mainfile_name}
Replace mainfile_name with your main file.

Categories