Compiling multiple packages using the command line in Java - 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.

Related

Create .jar file idea project command line

I want to create a jar file from this project https://github.com/5up3rc/weblogic_cmd
I have seen that there is a weblogic_cmd/.idea/artifacts/weblogic_cmd_jar.xml file which is probably what I need but I haven't seen any tutorial on how to do it via command line.
I have also tried to compile it manually
find src -name \*.java -print0 | xargs -0 javac -cp -classpath ./lib/*.jar -o ./classes/
but I failed.

Beginner: Need to Run a Makefile in Windows

I am currently trying to install a program from Sourceforge. The installation process requires that I change directory paths in the makefile and execute it. I have been researching how to run the makefile but I am still lost and was hoping to get some help or be pointed in the right direction. I am running Windows 8.1. I believe the program is going to execute a java class file. However, I am unfamiliar with the language and not sure how I should alter the makefile to run in windows 8.1. I tried to run "make" in the command prompt but that didn't work. I posted the makefile below. I understand I should change the directory for ImageJ (which I have installed). But I'm not sure how I should alter path for windows and how I can then execute it. The manual instructs me to change IMAGEJ_DIR to the installation place of the program ImageJ. Then execute "make" and "make install" will generate the jar file. "My imageJ folder is located at C:\Program Files. Do I need to change the directory to execute this makefile, and if so how can I do that in windows? The makefile itself is located in my downloads folder. Any help would be greatly appreciated. Thanks!
JC=javac IMAGEJ_DIR=/home/wenja/ImageJ PLUGIN_DIR=$(IMAGEJ_DIR)/plugins/OpenBeamProfiler IJ_JAR=$(IMAGEJ_DIR)/ij.jar APACHEMATH_JAR=$(IMAGEJ_DIR)/plugins/commons-math3-3.2.jar LIBJAR=$(IJ_JAR):$(APACHEMATH_JAR) CLASSPATH=$(IMAGEJ_DIR):./:$(LIBJAR) CLASSDIR=./classes COMPILEFLAGS=
COMPILEFLAGS=-Xlint:deprecation
COMPILEFLAGS=-Xlint:unchecked
all: $(CLASSDIR)/BeamProfiler_Plugin.class $(CLASSDIR)/BeamProfilerFrame.class $(CLASSDIR)/BeamProfilerResultFrame.class $(CLASSDIR)/BeamViewWindow.class $(CLASSDIR)/BeamFunctionGauss.class $(CLASSDIR)/BeamFunctionSuperGauss.class
clean: rm -f $(CLASSDIR)/*.class rm -f *.jar
$(CLASSDIR)/BeamProfiler_Plugin.class: BeamProfiler_Plugin.java $(JC) $(COMPILEFLAGS) -cp $(CLASSPATH) -d $(CLASSDIR) BeamProfiler_Plugin.java
$(CLASSDIR)/BeamProfilerFrame.class: BeamProfilerFrame.java $(JC) $(COMPILEFLAGS) -cp $(CLASSPATH) -d $(CLASSDIR) BeamProfilerFrame.java
$(CLASSDIR)/BeamProfilerResultFrame.class: BeamProfilerResultFrame.java $(JC) $(COMPILEFLAGS) -cp $(CLASSPATH) -d $(CLASSDIR) BeamProfilerResultFrame.java
$(CLASSDIR)/BeamViewWindow.class: BeamViewWindow.java $(JC) $(COMPILEFLAGS) -cp $(CLASSPATH) -d $(CLASSDIR) BeamViewWindow.java
$(CLASSDIR)/BeamFunctionGauss.class: BeamFunctionGauss.java $(JC) $(COMPILEFLAGS) -cp $(CLASSPATH) -d $(CLASSDIR) BeamFunctionGauss.java
$(CLASSDIR)/BeamFunctionSuperGauss.class: BeamFunctionSuperGauss.java $(JC) $(COMPILEFLAGS) -cp $(CLASSPATH) -d $(CLASSDIR) BeamFunctionSuperGauss.java
Note: the "_" in the jar file name in important to be recognized by ImageJ
for including manifest use
jar cfm Beam_Profiler.jar Manifest.txt BeamProfiler_Plugin.class BeamProfilerFrame.class BeamProfilerResultFrame.class
it is important to jar *.class to also get ...$1.class etc.
jar: all cd $(CLASSDIR) &&\ jar cf Beam_Profiler.jar * &&\ mv Beam_Profiler.jar ../
install: jar #if test ! -d $(PLUGIN_DIR); then mkdir $(PLUGIN_DIR); fi
cp Beam_Profiler.jar $(PLUGIN_DIR)
uninstall: rm -f $(PLUGIN_DIR)/Beam_Profiler.jar
Try this,
Open command prompt => Go to windows start menu (run programs) -> type the command "cmd.exe"; this will list the command prompt program in the list. Open the command prompt. Probably it'll be defaulted to your home directory. So issue the command "cd Downloads\directory_of_the_program_to_install" to navigate to the program directory. Now go to the real program folder, then find that make file, open it in edit mode, and then change the entry
IMAGEJ_DIR=c:\program files\imagej_directory. Then go to the command prompt that we opened before, and issue the command make and then makeinstall. This would do the trick.

How to translate an entire folder or package from Java to Objective-C?

I've used an older version of Google's Java to Objective-C (J2ObjC) converter previously (i.e. version 0.5.2) and it was straightforward to translate an entire folder of Java files to their equivalent Objective-C files (and to preserve the directory structure in doing so). I just had to run the following shell executable:
$ ./run.sh —-preservedirs <path to input folder>
I've just downloaded the latest version of J2ObjC (i.e. version 0.9.1) and it's not clear from the Getting Started page or elsewhere how I can translate an entire folder of Java files rather than just a single Java file using the j2obc executable. The only example provided in the Getting Started page is to translate a single Java file which has no dependencies or imports elsewhere as follows:
$ ./j2objc Hello.java
Can anyone provide me with an example of how to translate an entire package assuming I have a folder named input which contains my com package which contains all of the sub-packages and Java files that I want to translate?
To build a whole project, I add the source root(s) to the -sourcepath, then use the find command to locate all Java sources. For example, to build Square.com's Dagger library:
$ export J2OBJC=~/j2objc # change to wherever your distribution is
$ cd ~/src/dagger/core
$ $J2OBJC/j2objc -d build_output -sourcepath src/main/java \
-classpath $J2OBJC/lib/javax-inject.jar \
`find src/main/java -name '*.java'`
All the generated .h and .m files are now in the build_output directory, in subdirectories according to their package (like javac does). To compile all the .m files into a static library, I use:
$ cd build_output
$ j2objcc -c -I. `find . -name '*.m'`
$ libtool -static -o libdagger.a *.o
If there is no better way built into run.sh, you could use find's -exec flag:
find <path to input folder> -type f -exec --preservedirs ./run.sh {} \;
Or, you could use xargs to do multiple files at the same type:
find <path to input folder> -type f | xargs ./run.sh --preservedirs
(You might also need to add -name "*.java" to the find arguments if there are non-Java files in your directories).

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

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