How to compile multiple class files in Java on LINUX shell? - java

Hello I am on campus trying to compile a simple binary tree program .. our campus only has shell and I am using Linux over eclipse..
I have 2 class files in my current directory bintree.java and treetest.java
javac bintree.java treetest.java
this code creates multiple classes but what is my next step? ive searched everywhere theres not a lot of info on java Linux shell. thank you

If all of the java files you need to compile are in your directory you can
javac *.java
And then
java NameOfClassWithMainMethod
Otherwise if you want to learn to work without an IDE I would suggest learning to use Maven or Gradle. They will abstract away a lot of the tedium of compiling a project, and if become a pro dev you'll need to know at least Maven anyway.

$ find -name "*.java" > sources.txt
$ javac #sources.txt

You might look at http://www.dummies.com/how-to/content/how-to-use-the-javac-command.html
When you run javac xxx.java xxy.java xxz.java you should get several .class files as a result. Is your problem really with running the javac or getting the resultant classes to run your program?
If so you may want to look here at another stackoverflow questin
Basically use java -cp classname for the class that has your "static Main()" in it

Related

How is Java's CLASSPATH used on a Mac?

I'm currently learning java from a book and I just reached packages. I've been saving all my files on my desktop and compiling/running programs from Mac's Terminal console.
John-MacBook-Pro:~ john$ cd desktop
John-MacBook-Pro:desktop john$ javac Learning.java.
John-MacBook-Pro:desktop john$ java Learning
.... program executes and so on .....
Now I save my .java files into a package (create a new folder). Let's call the package 'book' And I'm told to run programs like this now:
javac book/Learning.java
java book.Learning
This works when I have one folder, sure, but when subclasses and more packages are added into that book folder how do I compile things deeper in? Not to mention how to run them afterwards?
The book might have assume prior knowledge so it just dives right in and tells me to setup CLASSPATH or use -classpath on my Macbook before attempting. I've tried various commands on terminal and it seems to compile sometimes where I have to manually change directory to open each folder (which is a lot of typed commands). Trying to run any classes always result in class not found. Every other answer seems to have some of the basic stuff setup already or is explained in terminology I don't understand yet.
When more classes are added, you compile them all:
javac book/Learning.java book/chapter/Chapter.java ...
You run the main class exactly the same way:
java book.Learning
If you're not in the package where the root of the package tree is (i.e. your desktop directory), you pass it in the classpath:
java -classpath /users/Leosam/desktop book.Learning
Note that it works on macOS the same way as on any other platform.

Java: Creating an executable in bash/command line from several files and packages

I've looked over the questions I could find in the search concerning javac, Java compilation and so on, but they either didn't answer this combination of questions, or the solutions didn't work.
I have a project that works and compiles fine in my IDE. However, it has to be compiled and executed through bash commands. Using an IDE, Ant, Maven or any other build tool is not an option. At best, I can use a makefile but from what I've gathered from concerning Java and make around here, this is generally a bad idea. The fact that I have absolutely no idea about Unix doesn't help the matter either.
My project consists of a good number classes split up into three packages, but as a last resort I could still dump it into one package if that'd make it any easier, but I've pretty much exhausted my options to try and solve this myself. Including the commands for Windows would be appreciated (because it would make it easier to test), but isn't necessary.
Try compiling your classes in separate package like this
javac [path to folder1]/*.java [path to folder2]/*.java
OR
In unix,You can list down all java files in a single source file say projectsource.txt
and try following command
$ find -name "*.java" > projectsource.txt
$ javac #projectsource.txt

How to compile and run a java file that contain weka libraries in dos?

I am not much familiar with java but i know the usual method we are compile a java file is
path = C:\Program Files\Java\jdk1.8.0_25\bin
javac main.java
java main
But when i try that weka libraries do not working. I am using jre 1.7 with weka for my program . Please anyone enlighten me how to include the weka libraries and compile the java code.
You need to know two things .
how to add a directory to a class path .
where your weka.jar and/or weka-src.jar files are
Now ,
STEP 1. COMPILE THE CODE
To compile the code you need to give the following command .
javac -classpath "path/to/lib1:path/to/lib2" main.java
This line tells javac to compile 'main.java' and use the libraries if it needs to .
STEP 2.RUN THE CODE
To run the code you just need to the same thing as above but along with the path/to/libs you should also add the path/to/yourpresentdirectory/ just to be on the safe side as there might be some extra .class files generated after compilation that JVM need to use .
java -classpath "path/to/lib1.jar:path/to/lib2.jar:path/to/yourpresentdirectory/" main
This should do the trick.
PS: I have given the commands for linux (i am using ubuntu 14.04). They should work just fine on Windows with slight changes ( you need to use '\'instead of '/' and use ';' instead of ':' where you separate the paths).

Help Request: Getting Batch Java Compiler Finished To Work

Ok, So I have been making this All-in-One Batch File Compiler for Java Files, to compile the Class Files, Manifest files, and the Jar Files. Everything works but one thing... I can't seem to figure out how to call another path in certain cases.. I will explain a case example below the Batch Code:
# echo off
COLOR 0a
Title Leaum's All-In-One Java Compiler
echo Compiling The Class Files...
"Whattt to do here?!" C:\JavaApps
javac -classpath . *.java
Pause
cls
echo Type the CLASS File Name Exactly...
set Man=
set /p Man=Type CLASS File Name: %=%
pause
echo Compiling The Manfiest File...
echo Main-Class: %Man%>>manifest.txt
pause
cls
I posted it in pastebin because the code thing was being weird.
Anyway, Line 6 for example, let's say I want it to call that Folder to Search for certain .Java Files when it needs to. When ever I am compiling a Java file that contains for example "TextIO.putln" Code, I would need it to call the TextIO.java/Class file in order for it to compile correctly, and I just want to store all the standalone/applet files in a separate directory... And have it include that in the Class Compiler, But still create the Class Files in what ever directory the Current project .Java is in.. If any of this makes sense? I can't seem to find a way to make it call the TextIO though :3.
Any help would be great. Thank you!
Don't re-invent this wheel, especially in a platform specific language!
Learn Maven 3 or at least Ant or even Gradle. SCons would even be preferable to Windows specific batch files, there is nothing of value to learn from the approach you are taking.
If I'm understanding your question right, you simply need to add the directory containing your utility classes to the classpath for the compiler, e.g.:
javac -classpath .;C:\JavaApps *.java
But I do agree with Jarrod and others that, in the long run, it is better to learn to use a build tool.

Problems compiling file on School's Unix System

COMPLETE EDIT BUT SIMILAR PROBLEM
What's the best software/plugin to enable FTP on Eclipse? I'm using FileZilla, but is there something better/easier?
You are telling javac to compile gamedata.txt and it is reporting an error that it cannot compile this file.
I'd highly suggest using a tool like Ant to script your compilation/packaging/etc so you don't have to worry about typing in arguments on the command line.
First of all, the -J command line argument is not meant to be literally passed as -J<flag>. Taken directly from the javac man page (you can view the exact same thing by typing man javac into the shell):
-Joption
Pass option to the java launcher called by javac. For example,
-J-Xms48m sets the startup memory to 48 megabytes. Although it
does not begin with -X, it is not a `standard option' of javac.
It is a common convention for -J to pass options to the underly-
ing VM executing applications written in Java.
Really, if you want to make this an executable, you can just use the tools that exist in Eclipse to make an executable. Using the command-line javac adds an extra level of complexity that is unnecessary, and that Eclipse is specifically designed to remove.
In eclipse, you can (I think) use File->Export->Java->Executable JAR File to make your project into an executable JAR that any computer with the Java Virtual Machine can run. That way, your project will work on both your computer and the Unix system at your school. You may have to add gameData.txt manually to the JAR or include it separately in the package, not sure how Eclipse does that type of thing though.
You can only compile .java files. If you remove the .txt file from the list of files to compile, it should work fine. If you want to compile all the files in a directory, you can simply use javac *.java
There are some examples in the javac synopsis.
Edit: Updated link to Solaris examples, which are similar to Linux.

Categories