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

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.

Related

How do I execute a shell command path after creating a new directory in Makefile?

I'm trying to get an updated path of my variable service_SOURCES, after I have generated some files from jaxb with my makefile:
service.jar$(EXEEXT): $(service_SOURCES)
mkdir -p bin
xjc -d src -p gen.files ./src/resources/info.xsd
javac -cp service_SOURCES
service_SOURCES := $(shell find ./src -name "*.java")
I am trying to compile my existing java code with my generated java code Currently, my service_SOURCES variable finds my folder containing my java files, and compiles them all together. I need it to update to include the newly generated java folder, so it compiles my new java files along with my old java files.
I've tried linking the above commands together using && but the shell command still doesn't update. If I run the above commands in terminal it works, but when I run it in my Makefile the path won't update properly.
(moving to answer as this doesn't fit in comment)
You should understand that Make runs in two phases -- The dependency tree is built in the first phase, and the rules are run in the second. This means that the dependency tree cannot be updated by a shell command run after files are generated by a recipe. If you want to do something like this (and I agree with #JohnBollinger, this might not be the best idea), you might need to split your logic between two makefiles, or have a self-recursive makefile. Either way you need to generate the files, then call the second makefile, which can then build a new dependency tree based on a timely shell command.
Alternative:
service.jar$(EXEEXT): _target_to_generate_java_files_
mkdir -p bin
xjc -d src -p gen.files ./src/resources/info.xsd
javac -cp $$(find ./src -name "*.java")

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.

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

How to make a proper Java makefile?

I need to make a makefile for a Java project.
My project is basic. A package which contains my main file and some others packages.
Can someone help me to make a proper makefile for that kind of project ?
Sorry I have not tested so it is likely to fail...
Assuming that what you need is to generate a executable jar file the following should work but I have not tested it.
The Makefile below assumes that your sources are located under ./src and that you are happy to use ./build for intermediary files (which is totally obliterated by the clean target so be careful).
Then 'make' or 'make jar' should generate the jar file.
NAME=MyProject
MAIN_CLASS=MyMainClass
SRC_DIR=./src
CLS_DIR=./build/classes
MANIFEST_FILE=./build/META-INF.MF
JAR_FILE=./$(NAME).jar
SRC_FILES=$(shell find $(SRC_DIR) -iname "*.java")
CLS_FILES=$(patsubst $(SRC_DIR)/%,$(CLS_DIR)/%,$(patsubst %.java,%.class,$(SRC_FILES)))
.PHONY: jar run clean mrproper
jar : $(JAR_FILE)
run : $(JAR_FILE)
java -jar $(JAR_FILE)
$(JAR_FILE) : $(MANIFEST_FILE) $(CLS_FILES)
jar cmf $< $# $(CLS_DIR)
$(MANIFEST_FILE) :
mkdir -p $(dir $#)
echo Main-Class: $(MAIN_CLASS) > $#
$(CLS_DIR) :
mkdir -p $(CLS_DIR)
$(CLS_DIR)/%.class : $(SRC_DIR)/%.java $(CLS_DIR)
javac -d $(CLS_DIR) -sourcepath $(SRC_DIR) $<
clean :
rm -Rf ./build
mrproper : clean
rm -f $(JAR_FILE)
Each time you execute Make it performs a find operation to get the list of source files, that might cause some delay depending on how many files and how fast is the file-system.... but you said is is a small project so it should not be an issue.
The double patsubst might well be compressed into a single one but I did it in two steps just in case.
Also notice that this solution compiles each Java class separately. This can be quite costly and it might be advisable to have another target to compile all at once ideally by creating a file that contains the name of all src java files and passing it to javac.

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