Java make batch file for multiple package structure? - java

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

Related

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.

convert windows javac command to linux

I'm having troubles with converting my javac command as used under windows (my libraries are placed within the lib folder, the source code within the src folder, and the compiled code should be placed within the bin folder):
javac -cp lib/\* -d bin src/\*.java
into the correct linux javac command. I have tried many variations including
javac -cp lib/* -d bin src/*.java
but that doesn't seem to work :
"invalid flag: lib/org.eclipse.paho.client.mqttv3-1.0.2.jar".
I am looking for a solution to this already quite some time, so any advice on this would be highly appreciated!
This is one of those odd things that happens when you don't put quotes around an argument.
Solution worked out in comments section: "lib/" instead of lib/
javac -cp "lib/*" -d bin src/*.java
The other issue was that ; is needed instead of : in Linux for classpaths.

How do I link jar packages together with *.java files during compilation using GCJ?

I have the following files:
A.jar (containing *.class files)
B.jar (containing *.class files)
Program.java (containing Program class with main function, which depends on A.jar and B.jar)
How can I build an executable file Program using GCJ?
It's been a while since I played around with Java so the following are mostly off the top of my head.
In linux usually a java program is launched by a wrapper script. For your case this wrapper script can be the Program, the content:
#!/bin/sh
java -cp A.jar:B.jar:/path/to/dir/where/Program.class/is/in Program
If you prefer to have only a single jar file then you can "unjar" A.jar and B.jar and create a new jar, say Program.jar that contain all the classes from A.jar, B.jar and your Program.class, and you create a little manifest file that tells which class is to be run when you execute the jar file (in this case it's your Program.class).
The content of the manifest file (let's call it manifest.txt):
-----8<------
Main-Class: Program
----->8------
Note the blank line after the "Main-Class: Program" line - it's needed.
So the create the single Program.jar:
gcj --classpath A.jar:B.jar Program.java
mkdir tmp
cd tmp
jar xf ../A.jar
jar xf ../B.jar
cp ../Program.class .
jar cmf ../manifest.txt ../Program.jar .
cd ..
Now create the shell script wrapper Program:
#!/bin/sh
java -jar /path/to/Program.jar
Make it executable:
chmod +x Program
and run it:
./Program
Applause if it works, throw rotten tomatoes otherwise!
This works for me:
gcj -c A.jar -o A.o
gcj -c B.jar -o B.o
gcj --main=Program --classpath=A.jar:B.jar -o Program A.o B.o Program.java

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.

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