compile sample.java and jgraph_5.8.3.1.jar - java

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.

Related

Javac classpath / cp option not able to find the source file

I have a source file Example.java in the following location:
C:\Users\sushr\Desktop\Experimental Java code\tutorial
Result of dir command from tutorial directory:
Directory of C:\Users\sushr\Desktop\Experimental Java code\tutorial
10/10/2020 01:51 PM <DIR> .
10/10/2020 01:51 PM <DIR> ..
10/10/2020 01:51 PM 133 Example.java <- This is the source file
I am trying to compile this file from location C:\ .
The command that I am running from the command prompt is the following:
C:\>javac -cp "C:\Users\sushr\Desktop\Experimental Java code\tutorial" Example.java
I am getting the following error:
error: file not found: Example.java
Usage: javac <options> <source files>
use --help for a list of possible options
The classpath setting for javac is for finding other libraries and classes while compiling your .java files. It is not used for finding the .java files you specified as argument for the javac program. When you call javac Example.java and you are currently in the directory C:\, then it will look for a file C:\Example.java. It is most likely that the Example.java file will not be directly in the file system root C:\.
Either specify the .java files with an absolute path or adjust your working directory with cd "C:\Users\sushr\Desktop\Experimental Java code\tutorial\" to go in that directory and compile the files from that location.
If you specify the absolute path to your .java file you should be able to just compile it without the -cp flag like so:
C:>javac "C:\Users\sushr\Desktop\Experimental Java code\tutorial\Example.java"
In macOS, I have noticed that using the "~/" to shortcut to home, it does not work. For example:
javac -cp .:~/algs4/algs4.jar MyFile.java
Instead, I had to use the full path to locate the .jar file in order to compile:
javac -cp .:/Users/username/algs4/algs4.jar MyFile.java

how can I run java package using the Unix command line?

Given that I have the following file structure,
Anybody know what Unix commands to run the java project or any resources I can look up in order to figure this out?
For compiling both the java files:
EatInTime-SpringBootGrab_Data>javac -d "classes" "src/main/java/DataGrabber/Serial.java"
EatInTime-SpringBootGrab_Data>javac -d "classes" -classpath "src/main/java/DataGrabber/Main.java"
For Executing Main:
EatInTime-SpringBootGrab_Data>java -classpath "classes" main.java.DataGrabber.Main
Hope this helps.

Java make batch file for multiple package structure?

I am trying to make a batch file to create my client side application window, I am using multiple packages.
src/nu/connect/client/* contains all the logic and chat window itself.
src/nu/connect/message/* contains the MessageStructure class file.
javac -d bin src\nu\connect\client\*.java
javac -d bin -cp bin src\nu\connect\client\ChatWindow.java
java -cp bin nu.connect.client.ChatWindow
pause
Here is the error I am getting,when i run the batch file:
src\nu\connect\client\ChatWindow.java:7: error: package com.message does not exist.
Here is the solution to my problem, had to compile both packages in one line.
javac -d bin src\nu\connect\client\*.java src\nu\connect\message\*.java
javac -d bin -cp bin nu\connect\client\ChatWindow.java
java -cp bin nu.connect.client.ChatWindow
pause

Simple clarification on javac command

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.

compile files from different directories with javac, referring a depending jar file?

I have the following set up:
I have 4 packages:
root/src/terminal - has some java files
root/src/mail - has some java files
root/src/data - has some java files
root/src/main - has a single java file, Main.java
I also have the following files
root/bin - a folder to store .class files
root/mail.jar - a jar file which has important classes used in my code
Within the root, I would like to enter a terminal command which compiles root/src/main/Main.java and puts the class files in the root/bin location.
Can someone show me the command to do this? I'm on a Mac (running Leopard).
Here's the one liner:
cd /xyz/root
rm -rf bin/*
javac -d bin -classpath mail.jar -sourcepath src main/Main.java
Alternatively, you could use absolute directory names:
rm -rf /xyz/root/bin/*
javac -d /xyz/root/bin -classpath /xyz/root/mail.jar \
-sourcepath /xyz/root/src /xyz/root/ main/Main.java
In reference to Ant you said "I would rather keep it simple.".
In fact in the long term it is simpler to create a simple Ant build.xml file. The alternative is a bunch of non-portable scripts or batch file ... or lots of typing.
To run the application, assuming that you are still in the /xyz/root directory:
java -classpath bin:mail.jar main.Main
Or on Windows:
java -classpath bin;mail.jar main.Main
Or modify the above to use absolute pathnames in the classpath argument; e.g.
java -classpath /xyz/root/bin:/xyz/root/mail.jar main.Main
Without knowing your operating system?
What you should look into is using Apache Ant. It is a build tool that once installed and configured can utilize a build.xml file in your root to compile class files to a folder as well as package a jar file.
http://ant.apache.org/
try this:
javac -cp "/root/mail.jar;/root/src;" -d "/root/bin" Main.java
This is written hoping that you have package declarations in your classes from src folder like package terminal; and package main;.
See this: Options in javac command
Or use Apache Ant as suggested by maple_shaft.
From comment give by #maple_shaft:
In Unix, Linux operating systems the classpath separator is a colon instead of a semicolon.

Categories