Cannot run this program from cmd with classpath option - java

this is a beginner question. I am having a problem running my java program from command line. I am using Windows10.
The problem is the following. I have a folder named "folder1", which is located o the dekstop of my computer. So the full path would be
C:\Users\Ioanna\Desktop\folder1
Inside that folder I have created a second folder which I named folder2. so the path to this would be
C:\Users\Ioanna\Desktop\folder1\folder2
Inside folder2 I have a java file named example.java
I want to compile it and run this file with setting the -classpath option through cmd. I dont want to set the path or to add the folder to tha path from environment variables.
I am trying
C:\Users\Ioanna\javac -cp C:\Users\Ioanna\Desktop\folder1\folder2 example.java
but it says file not found.
I tried several other alternatives, but I can't seem to find how to compile successfully the program.

Code compilation (to bytecode) and code execution are two separate steps, in Java.
First, compile your .java to obtain the corresponding .class file (I'm assuming your folder paths are right):
C:\Users\Ioanna\javac C:\Users\Ioanna\Desktop\folder1\folder2\example.java
This will give you example.class in that same folder.
Next, run that class (provided it has a main() method):
C:\Users\Ioanna\java -cp C:\Users\Ioanna\Desktop\folder1\folder2 example

java expects the path of the file(s) to compile. And example.java is not in the current folder (C:\Users\Ioanna).
Use
javac Desktop\folder1\folder2\example.java

Related

Java can't compile in terminal because of external libraries

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>

(JAVA) Use Command Prompt to create .jar file from multiple .class files

I have written a .java file, called Main.java, and have compiled it using the javac in the Windows Command Prompt. The compiler is creating multiple .class files (called Main.class, Main$1.class, & Main$2.class--presumably because I have anonymous inner classes in my Main.java file). I am trying to create a runnable .jar file so I can double click a shortcut to run this application (it is a Java Swing application), but I am unsuccessful when I navigate to the directory of the three class files and type:
jar cfv file.jar Main.class Main$1.class Main$2.class
The Command Prompt then outputs this text:
added manifest
adding: Main.class(in 4871) (out = 2848)(deflated 41%)
adding: Main$1.class(in 1409) (out = 833)(deflated 40%)
adding: Main$2.class(in 1239) (out = 767)(deflated 38%)
Despite this, when I double click on the file.jar file in Windows Explorer, simply put, nothing happens. No swing application opens.
Hopefully someone can help me out with this. Thank you
Best...SL
You need to use the entry-point switch -e (with the name of the class containing the main() method) as such:
jar cfve file.jar Main Main.class Main$1.class Main$2.class
Something's gotta tell the java which class should be started automatically.
That's the Manifest - see description here
You have to package the Manifest.mf in your jar.
Open Command Prompt
cd go to the path where jar file exist
run command jar xf fileName.jar
It will generate com, META-INF & Copyright.mk files
Go to the particular package where u want to modify the class file (Basically class files present in com directory)
Download the JAD.exe from google (Its Zip File)
Extract the Zip file you will get jad.exe
Place the class file (which u want to modify) in the folder, that contains jad.exe
Go to command prompt, run jad fileName.class (the class filename which u want to modify)
fileName.jad file will get generate , rename it as fileName.java
compile the fileName.java (if its referring to some other class file configure it in eclipse accordingly)
once compiled replace the class file in the directory defined in step 4
Go to command prompt, cd Step 4 path, run jar cf fileName.jar (* represents all the class files irrespective of directories)
open cmd (windows+r type cmd)
simipily type command as a "jar" without quatoes
it will give some options and those usage.
use what you want
to create a jar file with class you to mention jar cfv filename.jar example.class
example2.class etc..
IF your using swing compile it with javac -deprecation SwingApplication.java but JDK version must be greater than version JDK1.1
run java SwingApplication
ALL THE BEst

java command line argument to make jar find files in two directories?

I have a jar file which I do not have the source code but want to use.
The jar file prompts for a file to read and generates an output file using a combinatin of the input file and a number of 'helper' files it uses for data. It works perfecty fine if run from its expected home directory, but I'm trying to write a script which will allow running the jar from anywhere.
The problem is that if I try running the jar file from anywhere other then its home directories it fails to find the support files it needs to properly generate its data.
If I run the file from its expected home directory I have to give the full address of the input file or it won't find it. I would prefer to be able to give just the relative path and Java know to look at whatever directory the person calling my script is in.
Is there a way I can have a bash script pass a command line argument to Java that would ensure that this jar looks at both of the relevant directories (directory of the helper files and the current dir of the person calling the script) when trying to resolve a relative file path? Something like the -classpath argument?
With the --classpath (or -cp) you can tell your Java program where it should take the dependency classes. So, probably if you do like in your files directory
$JAVA_HOME/bin/java -cp '.:/path/to/the/original/program' My.class myfile.txt
then it will wind the program, and find your files as well.
UPDATE
If it doesn't work, you can try to force the file loading some other way. The Javadoc says:
By default the classes in the java.io package always
resolve relative pathnames against the current user directory. This
directory is named by the system property user.dir, and
is typically the directory in which the Java virtual machine was
invoked.
So, you can try running the program from the original directory this way:
$JAVA_HOME/bin/java -Duser.dir=/path/to/the/files/directory My.class myfile.txt
UPDATE2:
As I wrote in a comment, you can try symlinks. Execute the following commands in the original directory:
ln -s /path/to/the/files/directory datafiles
$JAVA_HOME/bin/java My.class datafiles/myfile.txt
Sorry - ignore. I missed the first line of your question.
You could pass the two paths as an argument to the jar file - then append the path location at runtime. Many ways to do that, here is one:
java -DdirectoryA="/somewhere" -DdirectoryB="/elsewhere" -jar program.jar
and in your code
String pathA = System.getProperty("directoryA");

Error when executing a JAR file

I've been learning about JAR files and wanted to try and create and run one myself. I carried out the following steps:
Created a project folder with a 'source' subfolder and a 'classes' subfolder
I wrote 2 source files, one with a main method which creates an instance of the other class and runs a simple method in it.
Compiled these to the 'classes' subfolder. I checked to see if they would run. They did
I created a manifest.txt file and filled in the Main-Class: xxxx and hit the return key. I saved this in the sources subfolder
Created a jar file in the classes subfolder by writing
jar -cvmf manifest.txt zzz.jar *.class
Tried to execute the jar file by typing
java -jar zzz.jar
This gives a ClassNotFound exception. If I try to execute the jar by double clicking on it in windows I get an errorbox saying "Could not find the main class xxxx"
I've double checked the spelling of the class inside the manifest file and it's correct.
Possibly important: I have to compile my programs using java -cp . xyz as there is an issue with my classpath. Does this mean that I need to execute jars in a different way as well? I tried
java -cp . -jar zzz.jar
but ended up with the same exception.
Edit: I ended up starting from scratch and now it runs (with the basic -jar zzz.jar command). Frustrating that I don't know what I was doing wrong but glad that it is working!
Shouldn't number 5. be run in the classes subfolder, where all your class files are? And if your classes are in packages, which they should be, you'll likely want to use * instead of *.class..?
To check what your jar file contains you can run:
jar tf zzz.jar
You will probably have to supply the entire path of the .class file you wish to execute after the classpath. ie java -cp xxx.jar classes.mainProgram.class. Where classes is the name of the folder which contains your class files.

Again "wrong name" error when executing java program

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.

Categories