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.
Related
I'm on Ubuntu 22.04 trying to run my code in the terminal.
The program works without problems in VScode, also when running multiple instances.
The program is consisted of six class files.
The trouble occurs when I try and run it with terminal. When compiling the java file
with javac it shows errors at places where I use the external libraries.
If I compile it with VScode and run the class file in terminal, I get the following error java.lang.ClassNotFoundException
This is causing me problems since I'm also supposed to dockerize the program.
You can add the following code in your setting.json file "java.project.outputPath": "bin",
This will be the .class file generated by VS Code in the bin folder of the same directory when running the Java code.
You can use the java command after entering the file directory with the cd command.
This generally indicates that the class path with which you're compiling your program does not include the correct paths to your libraries. Assuming your libraries are jar files, your javac command should look something like this:
javac -cp libs/lib1.jar:libs/lib2.jar srcs/*.java
where libs/ is the relative path to your libraries and srcs/ is the relative path to your own java files.
And when you run the program, make sure your class path includes both the locations of your libraries and the location of your class files (which in this case would be the current directory):
java -cp .:libs/lib1.jar:libs/lib2.jar <MainClass>
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.
Normally I can use javac to compile and java to run perfectly fine, they are set correctly into my computer's classpath. In this instance however, one of my files Graph.java is trying to import a jar file in the same directory Heap.jar.
When I try to use javac *.java, it tells me package mycs1 does not exist.
Normally in my lab on Linux machines, we can use export CLASSPATH=mycs1.jar:. and then javac will work normally again, and just add more files if there are more jars.
My problem is that this command does not work on my home computer running Windows 10. I am trying to set the classpath of multiple jars. This is similar to this question:
Unable to execute jar file despite having PATH and CLASSPATH set
But despite reading that, I still cannot find a way to set the path of multiple jar files in such a way that javac will work.
Is this possible? If not, what alternative command can I use?
Yesterday I solved a problem with an answer here on stackoverflow. But I ended up with another problem, I will try to be clear:
I have a project folder in the /home/demo/Desktop/xlsToCsv/ directory where inside of it is the java file "xlsToCsv.java" and another directory with the external jars that I need in /home/demo/Desktop/xlsToCsv/jars.
Now I need to compile and run my program. Yesterday I ran a command that assumed that I was already inside of /home/demo/Desktop/xlsToCsv/, and the commands were:
javac -cp ".:./jars/*" xlsToCsv.java
java -cp ".:./jars/*" xlsToCsv
The problem was solved and I was able to run my program without any problems. But, my program was supposed to run from the root directory, ie, the directory where it is when I open the linux terminal without the need to make a "cd" command.
So, when I open the terminal the path to the .java file is:
/home/demo/Desktop/xlsToCsv/
And the path to jars folder is:
/home/demo/Desktop/xlsToCsv/jars/*
Can someone explain to me what I have to do, and the reason so? Because more that run the program, I want to know the reasons and understand the classpath mechanism in java.
Avoid using relative classpath. and instread of "./jars/" use the absolute path "/home/demo/Desktop/xlsToCsv/jars/"
EDIT:
javac -cp "/home/demo/Desktop/xlsToCsv/jars/*" /home/demo/Desktop/xlsToCsv/xlsToCsv.java
java -cp "/home/demo/Desktop/xlsToCsv/:/home/demo/Desktop/xlsToCsv/jars/*" xlsToCsv
With reference to this post
Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line
I did not understand how to solve the problem
Actually in my java source code there' s line :
package es_2011;
when I compile the program through JCreator everything works perfectly.
It creates a folder named es_2011 where to put .class files.
Also the executing operation goes smoothly, the program runs ok.
Now I'd like to use the command line only.
So I placed my java file in the directory where javac.exe is but whenever I try to compile I get the same error
The command I use is: javac ProgAudioJ.java
The path (where javac.exe is ) is : C:\Program files\Java\jdk1.6.0_22\bin
Is someone willing to help me understand in terms of exactly tell me what I have to do?
thanks very much...MAX
The setup used for the looks like this (under windows)
C:\classDir -> is the project
C:\classDir\testpackage -> is the only package used (the package "testpackage")
C:\classDir\testpackage\Main.class -> is the class with the main method inside (important: it is the .class and not .java)
The Main.class looks like following:
package testpackage;
public class Main {
public static void main(String[] args) {
System.out.println("Program started! ;-)");
}
}
go with your command prompt to:
c:\classDir> java testpackage.Main
the result:
Program started! ;-)
According to your problems that it starts in your IDE but not from the console:
- checked if you realy use the path to the .class files?
- with the console go to the directory of you .class files, not the project (e.g. in Eclipse it is the bin directory
- enter the full qualified class name (including packages seperated by . -> e.g. testpackage.Main
More infos can be found under:
http://www.oracle.com/technetwork/java/compile-136656.html
Hope it helped
MAX, if the class defines that it's inside the package es_2011, then it should be in a folder with the same name.
So in your case, put the ProgAudioJ.java in the folder es_2011 and then run
javac es_2011\ProgAudioJ.java
latter to run it, you need the command
java es_2011.ProgAudioJ
You should add javac.exe in your path .Edit your path variable and append path to jdk's bin
then put java file in a dir named es_2011 , as the package declaration is es_2011 then compile
c:\es_2011\javac YourJava.java
and now go back to C:
c:\java es_2001.Yourjava
After reading you other Post: "Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line" I guess you go to the directory es_2011 where your ProgAudioJ.class file is located and run
java ProgAudioJ
from that folder.
instaend you have to go to the folder above (cd ..) and run
java es_2011.ProgAudioJ
Each package in Java corresponds to a folder on the filesystem. So a package declaration such as com.stackoverflow would mean that the source classes need to be in a folder ./com/stackoverflow. Typically the whole project would have a separate src folder containing com/stackoverflow.
When you compile the Java classes you DO NOT need to put source files in the same directory as javac.exe, you do however need to make sure that javac.exe is in your operating systems PATH variable. This tells the operating system where it should look for executable files when a command is run, on a *nix machine this would usually be /usr/bin or just /bin but on Windows machine the executables normally live within the applications own directories, that is C:\Program Files\something. Assuming that you've installed JDK correctly, the javac.exe should already be in the PATH you can check this by opening the command line and just running javac (just like that). If you get some output then all is well, the system knows where to find javac.exe.
Next you will need to go to your project folder and type javac -d . src/com/stackoverflow/MainSO.java notice that is run from the project folder. This will create a folder called com in your project root and put the compiled classes in com/stackoverflow. The -d flag tells javac where to put the compiled classes, if you leave that out, the compiled classes will be where the sources are.
Then when you want to run the classes you type java com.stackoverflow.MainSO (no .class). Crucially this command will need to be ran in the directory that contains the root of the class hierarchy (that is the com folder containing the compiled classes). You can specify other places for java to look for the classes by providing a classpath to the java command with the -cp flag. By default the classpath will contain the directory the java command was ran in. Should your project have dependencies external .jar files for example you will need to provide every single one of them (with their full filepath) in the classpath (this goes for the compiler as well). The IDEs do this automatically for you.
Hope that helps.