How is Java's CLASSPATH used on a Mac? - java

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.

Related

Can't run java file (having trouble with classpath fixes)

I'm remoted into a Linux machine that I don't own from my Windows machine. I've got 2 java files:
DBConnect.java
Main.java
I compile and run them fine on my machine and in my IDE (I'm using NetBeans). When I copy them to the remote Linux machine, the place they're located is:
/home/NETID/myname/430
I compile them using:
javac *.java
They compile successfully, which creates two new files:
DBConnect.class
Main.class
I then attempt to run Main.class. With each of the following commands, I get the error "Error: Could not find or load main class Main"
java Main
java <pkg>.Main
java <pkg>/Main
In my case, in NetBeans, my project is called MyProject. In the directory structure on the left-hand side of the IDE window, there is MyProject. Under that is a file called Source Packages. In that file is another thing called pkgMyProject. When I expand that, I see my two java files.
Also, at the top of both Main.java and DBConnect.java, there is:
package pkgMyProject;
I've read some other questions here referring to this issue, but I'm having trouble understanding the answers. Also, many of the answers don't apply because I don't think I can set the classpath on the remote machine.
When I check my classpath in my terminal (when I'm remotely connected) using...
echo ${CLASSPATH}
...nothing is displayed.
How can I find my classpath, and how can I run these files?
I think you are missing the package folder. You need to create a directory in the Linux machine with the name pkgMyProject. Then, run javac pkgMyProject/*.java and run java -cp . pkgMyProject.Main after that.
Update: You can add the -cp to set the classpath to be used in the java command.

How to compile and run project with multiple classes in package?

I just realized I'm about to graduate and I still don't know how to handle this situation:
Say I have a java package named mystuff.project1 and inside the package I have Project1.java (which has the main method) and ThingThatDoesStuff.java . Both are public classes, have the package declaration at the top of the file, etc. I can debug this project fine in Eclipse.
For maximum simplicity, I move the project to C:\ so the java files are located in c:\mystuff\project1
I navigate into c:\mystuff\project1 and type javac *.java to compile the class files
Now how the blazes do I run my application?
java Project1 doesn't work
java mystuff.project1.Project1 doesn't work
java -cp . Project1 doesn't work
java -cp . mystuff.project1.Project1 doesn't work
All of the above give me "Error: Could not find or load main class"
I've been searching SO and elsewhere to try to understand this problem but I'm completely baffled.
I've:
cleared my CLASSPATH variable
triple-checked PATH etc
successfully tried compiling and running standalone class files that aren't in a package
I understand this is a stupid newbie question but I just can't figure it out. I realized every other time I've run into this problem I similarly couldn't find a solution and put all of the classes into a single file. I'd rather learn how to stop doing that now.
I think you have to change to C:\ and then enter:
javac mystuff\project1\*.java
java mystuff\project1\Project1
and this should work.
It's either that or:
javac mystuff/project1/*.java
java mystuff/project1/Project1

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

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

compiling multiple java classes in linux

i used netbeans to code the classes and they are all included in a package but when i try to compile the application class in linux it spits out errors on class definitions for the classes i am working with. points at the class names for the objects and says "cannot find symbol" i need help!!!
use javac -sourcepath < your source code path >
Better check -help option as it mostly solve your problems
cd to the directory containing your package then run:
javac -classpath . your_package_name/*
I'm not a Java guru, but I have a small java project that I developed years ago and have recently ported to compile with javac on Linux.
I got this to work in two different ways:
Created a single Java source file that held all of my classes
Put each of my classes in a separate file but all in the same directory
In each case, I can compile and run with the following:
javac *.java && java name_of_main_class
Notice that I did not specify a "-classpath" option when I compiled. I guess this works because I have not used a directory substructure or created a package. If you are dealing with those issues, this page appears to have some examples that may help you: Help with packages in java - import does not work
A key thing to understand about Java packages: They correspond to subdirectories where the classes are defined (or to JAR files which just bundle and compress those subdirectories into a single file). Therefore, anytime you specify the package keyword in your source, you need to make sure that the source files (and the class files) are distributed to subdirectories correspondingly. The -classpath option to javac may provide a workaround when subdirectory structures do not exactly match what is specified by the package keyword.
If you built the project using NetBeans, you can use Ant to build the project on command line. NetBeans generate Ant Build script.
just cd into the directory where the project is located then type 'ant'
it should build the project for you automagically

How to run Java Programs

I am just starting Java but I know Scheme...How do you run java code using emacs and putty? I made a program and saved it as first.java on emacs and when i try to open it in putty i did load "first.java" but nothing happened so how do you do this and can yo write Java code in Dr.Scheme?
First, you have to name your file the same as the public class that is contained in the file. Then, once your class is written, you need to compile it using javac and run the .class file that is produced using the command java [ClassName].
A few things to note is that you are going to make sure you have a Java Development Kit (JDK) and that your path is set up so that javac and java are on the path.
I would poke around the Java Tutorials, specifically the "Hello World" application example.
To run Test class, you need to compile the file and run it. Here is how you can do it from the command line.
javac Test.java
java Test
Unfortunately, I cannot comment on using DrScheme (which is a Scheme IDE) for developing Java. Do you mean that you want to invoke Test class from within Scheme?
I haven't touched Java in years, but Sun's Java tutorial is probably all you'll need:
Others have already said to make sure your class name matches the java source file name and to run javac on that, and then java on the resulting .class file. Also, make sure your classpath is properly set when you compile and run.

Categories