Makefile to compile Java with external library [duplicate] - java

This question already has an answer here:
how to make makefile for java with external jar file
(1 answer)
Closed 8 years ago.
I want to write a Makefile that compiles and runs the following code. My question here is how to add an external library using Make syntax?
javac -cp commons-cli-1.2.jar Iperfer.java
java -cp .:commons-cli-1.2.jar Iperfer -c -h localhost -p 1234 -t 12

My question here is how to add an external library using Make syntax?
The Answer is in the question. You would use the same command as you would from the command line ...
This is really a Java agnostic question. It is really about how to write a Makefile. But presumably if your teachers want you to use make to build Java, they will have provided you with a sample / template Makefile to get you started. If you showed us that template, we could tell you how best to modify it.
But assuming that you don't have a specific template in mind, then this a duplicate of an existing Question.

Related

Why do I get an exception when trying to run .class file made with Jython? How can I solve it? [duplicate]

This question already has an answer here:
Issues understanding how to use Jython
(1 answer)
Closed 4 years ago.
I just found out about Jython and I really wanted to use it. Please forgive me if I'm doing something wrong and please explain.
I made a simple python program to test Jython, test.py. When typing
jython test.py
in the command prompt it run normally. So I decided to make a Java .class file. Since jythonc doesn't exist anymore, I tried
jython -m py_compile test.py
and a .class file was created in the same directory called test$py.class.
(this is equivalent to
>>> import py_compile
>>> py_compile.compile('test.py')
as I tried as well)
However, when I tried
java test$py
it threw me an exception:
Error: Could not find or load main class test$py
Caused by: java.lang.NoClassDefFoundError: org/python/core/PyRunnable
(I also tried with classpath but it did the same exception)
What does it mean? How can I solve it?
Note: I've downloaded Jython 2.5.4rc1.
This could be a corrupted installation of jython as per simillar Linux: NoClassDefFoundError org/python/core/PyException #1006 issue. If you can find jython-standalone-2.5.3.jar and it's not corrupted you may want to try re-downloading and re-installing jython.

Able to compile java files, unable to run files [duplicate]

This question already has answers here:
Error: Could not find or load main class [duplicate]
(22 answers)
Closed 5 years ago.
I am trying to run these programs, but I am getting
Error: "Could not find or load main class"
Here's a screenshot of me trying to run the programs in cmd line:
Windows Powershell Screenshot:
This makes no sense to me seeing as how the files compiled just fine, which would imply that the main class was able to be found.
If anyone could explain what's going wrong I would appreciate it very much, thank you.
The UDPServer code:
The UDPClient code:
You have defined a package serverClient; at the top of both files.
So you should be having a directory named serverClient with your .class files.
if you wish to execute using java command line, you should execute from the src directory like this
PS ...\Programming Assignments\src > java serverClient.UDPServer
PS ...\Programming Assignments\src > java serverClient.UDPClient
It would be useful if you post your code here. But if I try a shot in the dark I would say that your filename doesn't match the classname in your Java file.
When running javac you pass in the paths to files you want to compile - hence those files are implicitly on the classpath. When running java you aren't passing in any files explicitly, so you have to include the current directory in the classpath in order for the JVM to know to look there.
$ javac Foo.java
$ java -cp . Foo
I use this Bash function pretty often for quick JVM/JDK experiments, if you want to try to replicate it in Powershell.
It's one of those cases where the current working directory probably should be on the classpath by default ~90% of the time, but it can't be in order to accommodate that last 10%. (Whether that's a good design decision or not is debatable, of course).
You are:
In the wrong directory. You should be in the directory that contains serverClient.
Using the wrong commands. They should be:
javac serverClient/*.java
java serverClient.UDPServer
java serverClient.UDPClient

Something like environment variable in Ubuntu [duplicate]

This question already has answers here:
How to install Java application to my linux system
(2 answers)
Closed 6 years ago.
I'm new to Linux and Ubuntu environment.
I'm calling a jar file like this :
java -jar app.jar /somearg /anotherarg
But I find this ugly and I want to call my jar file like :
MyApp /somearg /anotherarg
So I think I have to set a environment variable like MyApp = java -jar app.jar.
But I don't know how can I do it.If anyone can help me I'll be very pleased.Thanks.
Using
java -jar app.jar /somearg /anotherarg
instead of
myapp /somearg /anotherarg
saves you only 2 words, so not that much. Anyway if you run your application frequently it is a good idea to provide an alias. If you are using bash shell (echo $SHELL shows something like /bin/bash) then here's the command you can paste in your terminal:
alias myapp="java -jar /path/to/your/app/app.jar"
After that you can use
myapp /somearg /anotherarg
It is important to provide the whole path to your app.jar file if you run it from different locations. Also if you want that your alias is permament (i.e. it is always after you log in) just add the same line at the end of ~/.bashrc file, using for example:
pico ~/.bashrc
PS. You don't need slash symbol to provide parameters. In unix systems it is more common to use "-" to set argument options.
PSS. Unix systems are case sensitive and it is typical that command line programs are all written in lowercase.

How would i execute this command in java? [duplicate]

This question already has an answer here:
How do i run a UNIX terminal from Java and send commands to it?
(1 answer)
Closed 9 years ago.
How would i make a java program find and delete all the files in my computer that have the extension: .jar?
I have the command:
find . -type f -name "*.jar" -exec rm -i {} \;
I know how to execute commands, but i don't know how to tell it to execute this command.
Simply, you CAN't delete ALL .jar files on computer. Because your java program needs JVM to run and since jvm uses jar files present under lib of JRE insallation. And in order to run your program you need lib jar files.
Although, you can give it a try with example from mkyong.
http://www.mkyong.com/java/how-to-find-files-with-certain-extension-only/
You can use a FilenameFilter to see if files match your pattern. http://docs.oracle.com/javase/7/docs/api/java/io/FilenameFilter.html
Once you have found a file you want to delete, use File.delete() to actually delete it.
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html#delete%28%29

compiling java programs [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
javac not recognized
I'm starting in the world of programming in java and I have run across a big problem: i cant compile my programs. when I type the javac command in cmd.exe(im using Windows 7) it displays this:
'javac' is not recognized as an internal or external command, operable program or batch file.
So I can't even test my first program.
I found in a book the following command:
set path=%path%;c:\java\jdk1.6.0\bin
but it doesnt work either.
I downloaded the newest version of the Jdk package just 2 weeks ago.
Look through your C drive for the c:\java folder and make sure you have version 1.6.0 of the jdk
c:\java\jdk1.6.0\bin
This will only work if you give it a path that actually exists. If you don't have 1.6.0, then type in:
set path=%path%;c:\java\<YOUR_ACTUAL_JAVA_VERSION_DIRECTORY>\bin

Categories

Resources