javac HelloWorld.java
Following error message keeps coming out every time when I run in cmd
javac: file not found: HelloWorld.java
However, when I added my file location
javac D:\User\Documents\Project\HelloWorld.java
It runs successfully.
Why?
I have tried to edit variable value of PATH to D:\User\Documents\Project
But it did not work.
You seem to have never used the command prompt before. The command prompt has a "current directory", just like in a file explorer window, there is a "current directory", the directory that you are viewing right now.
The current directory is usually shown just to the left of the caret.
The current directory on the above photo is C:\Users\Mrhope.
Type dir to see all the files and directories of the current directory. You can then use cd to navigate to a subdirectory. Say you are in Documents now, and you can do this
cd Project
to go to the Project folder. By the way ../ means the parent directory
The HelloWorld.java in javac HelloWorld.java is a path relative to the current directory, so this will only work if HelloWorld.java is in the current directory. You should try to use cd to navigate to the correct directory then use that command.
Setting the PATH environmental variable is quite irrelevant here because that is setting the path of the JRE. Please set it back.
I suggest you learn the basics of cmd first. Many tutorials are available online.
You have to enter the directory of where your Java file is.
Fastest way is to open the folder where your Jar file is then hold the shift key on your PC and right click on an empty space in the folder (while still holding the shift key). When the context menu comes up look for the open command window here option (for newer Windows 10 versions it's power shell window), select the appropriate option. When it opens run your command
Related
This question already has answers here:
Import javax.vecmath
(6 answers)
Closed 1 year ago.
Preamble: So this all started with just trying to use javax.vecmath.Vector2d. I didn't have javax.vecmath so I spent a bit of time trying to get it, found that I needed to download Java3D.
After a long time of trying to download Java3D for Java (version 16.0.2), I eventually got it together with the vecmath.jar file landing in /Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home/lib/ext. This got rid of the error: package javax.vecmath does not exist error message.
Then, I got the message
<JAVA_HOME>/lib/ext exists, extensions mechanism no longer supported; Use -classpath instead.
.Error: Could not create the Java Virtual Machine.
this also wasn't letting me use any java commands in shell.
A bit of research and I concluded the solution to be moving (via Finder select and drag) j3dutils.jar, vecmath.jar, and j3dcore.jar over to lib and just deleting the lib/ext directory. I have gotten rid of the <JAVA_HOME>/lib/ext exists problem but back to error: package javax.vecmath does not exist.
I don't even know what to do know. I just want to use javax.vecmath. Am I going about this the totally wrong way? How can I get this to work?
Okay, I figured it out.
How to use javax.vecmath in Mac OS(11.5.1) with Java(16.0.2)
I am giving a description that sort of includes why I do things, skip to the TLDR at the bottom if you just want an answer.
Step 1: Download the latest version of Java3D
This contains vecmath, along with j3dcore and j3dutils. It will download a .zip file. Unzip the file and it will expand into a new directory with another .zip file inside, j3d-jre.zip. Unzip j3d-jre.zip and it will expand into a directory lib. Inside lib will be a subdirectory, ext, with three .jar files inside: j3dcore.jar, j3dutils.jar, and vecmath.jar. You can put these literally anywhere, just make sure you keep track of their location (I put them in ~/Library/Java/Extensions, this location is on the hard drive and will need an admin password to do anything–use
sudo unzip /path/to/j3d-jre.zip
if you are doing things in shell). You CAN put the ext directory in JAVA_HOME/lib/ but after Java 6, this will cause a problem.
Step 2: Change CLASSPATH
Java has no idea how to find vecmath.jar so you have to specify it.
Option 1: Specify CLASSPATH with every shell command
The simplest version is using
javac -cp ".:/path/to/vecmath.jar:" MyMainProgram.java
to compile and
java -cp ".:/path/to/vecmath.jar:" MyMainProgram
to run the program (you can also replace -cp with -classpath and it will do the same thing)
This option won't ever destroy your CLASSPATH but you also have to include the -cp command every time you compile and run a program that imports javax.vecmath.
Option 2: Specify CLASSPATH with every new terminal window
A little more lasting than -cp, you can define CLASSPATH such that any changes will only take place in that terminal window. Use this form:
export CLASSPATH=".:/path/to/vecmath.jar:"
Now when you call
javac MyMainProgram.java
java MyMainProgram
Java will see that CLASSPATH is .:/path/to/vecmath.jar and everything will compile and run without adding the -cp command.
The main downside of this option is that if you update CLASSPATH again, you have to remember to add the previous CLASSPATH (which you can see at any time with echo $CLASSPATH)
Option 3: Permanently add CLASSPATH to terminal
Enter the following into terminal:
open ~/.bash_profile
this will open a window that may or may not have code in it. Regardless of any pre-existing code, scroll to the bottom and add
export CLASSPATH=".:/path/to/vecmath.jar:"
This option holds the CLASSPATH in all terminal windows forever or until you change it (using any method above).
TLDR
Download Java3D for macOS
Unzip java3d-1_5_1-macosx.zip and open the directory it creates
Unzip j3d-jre.zip and open the new directory /lib/ and the subdirectory /lib/ext/
Move vecmath.jar, j3dcore.jar, and j3dmath.jar to ~/Library/Java/Extensions (this requires an admin password) or any other location
Run the following line in terminal:
open ~/.bash_profile
Go to the bottom and add the following:
export CLASSPATH="/path/to/vecmath.jar:$CLASSPATH"
import javax.vecmath.* to any .java program you want
The jar file can go where you want, moving it to your project's lib folder is good. The real issue is you need your classpath to point to it.
Here is a full explanation.
If you are running from the command line you don't need to set the classpath variable, you can provide it in the java command. It would be something like this:
java -cp lib/vecmath.jar Example
This assumes that the program you are working on has been compiled into a class file named Example.class. If you main method is in a package you will need to fully qualify the classname so it might look like:
java -cp lib/vecmath.jar com.demo.Example
You can list multiple jar files on the classpath, separated by a colon (:).
You can also ask for help in the command line by invoking:
java -h
I was able to run the JAR file inside IntelliJ when I do Shift + f10.
However when trying to execute the JAR file from my directory, nothing happens. My META-INF is place as followed here: https://stackoverflow.com/a/49147689/12973878.
This is my JAR structure
JAR structure
File structure Image
Image
Can I know what is the problem here?
Based on the description, I assume you're in a windows environment. Try to run it in terminal. Change the current directory to the location of the file and the terminal command you need to give is:
java -jar <jarfilename>
Some suggestions:
Use the -verbose:class option when starting the java application to determine if classes are loaded from the correct places.
See if there's additional settings in the run configuration of IntelliJ. You'll find it in the upper right corner of the editor window next to the executuin button - dropdown will show 'Edit Configurations'.
If there's really no error at all - are you sure you have the correct starting Class as entry point? You might simply be executing the wrong main method from terminal while
IntelliJ picks the correct one (look for "main class" in run configuration).
After running a build for the project (or a mvn package) your jar is placed in <your_project_path>/target.
There you will find: <your_project_name>-0.0.1-SNAPSHOT.jar. Now just open command prompt (on win) / terminal (on mac).
Go to the project path
cd <your_project_path>/target
and run
java -jar <your_project_name>-0.0.1-SNAPSHOT.jar
Don't forget to check that you have java path in your system $PATH
run to verify
java -version
javac not working in windows command prompt
^I tried the suggestions on this post, and it was SLIGHTLY helpful, but not completely.
I opened up my command prompt and I typed in "javac" after putting in the path in my Environment Variables and it didn't work, at which point I googled it and found that thread.
I knew that I had closed and re-opened my cmd already, and that didn't work, so I skipped that bit and I saw the part telling me to make sure that javac.exe exists, which I verified with the "dir" command in the cmd. Afterwords, while in the "C:\Program Files\Java\jdk1.7.0_25\bin" folder on the command prompt, I typed in the next bit of advice, which was
for %i in (javac.exe) do #echo %~$PATH:i
After entering this into my command prompt, I got the message "ECHO is on". Upon seeing this, I typed in "javac" again and this time, it worked. So I decided to test this out by backing out of the directory and going to a folder in which I had a .java file saved and running it, but it again told me that
'javac' is not recognized as an internal or external command, operable program or batch file.
This was disappointing. I think it'll only work if I'm INSIDE the bin file on the command prompt, which is annoying because I'm not an administrator on this computer and it will be annoying to always have to get admin permission (from my parents) to code. They will also never give me the password. Can anyone help me? Thanks in advance! And sorry for the huge wall of text...
EDIT: Someone has asked what would the output of "echo %path%" be. It is this:
C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\
^Is it not supposed to say this?
EDIT 2: #brano88...I think I did? I right clicked computer, went to properties, went to advanced system settings, clicked Environment Variables and went to the top part and pressed "New..." There, I entered the Variable Name as Path and the Variable Value as the location of the bin folder. Is this incorrect? I followed a YouTube tutorial step by step while doing this.
This one: http://www.youtube.com/watch?v=Hl-zzrqQoSE
How to run .java files from CMD
go to your Computer -> C: -> Program Files -> Java -> jdk1.7.0_25-bin
copy the path (example: C:\Program Files (x86)\Java\jdk1.7.0_25\bin)
Go to Control Panel -> System and Security -> System-Advance System
Settings -> Advanced -> Environment Variables
open the Environment Variables screen and go to System Variables and look for "Path"
after finding the Path system variable, double click it or press edit button and in the Variable value you paste the path from java you just copied after the last values already existing there.
Note!
make sure you DO NOT enter any extra space in this field as it won't work;
make sure you have one semi-colon before pasting the path, example: Path :
...%ANT_HOME%\bin;C:\Program Files (x86)\Java\jdk1.7.0_25\bin
Note! If you previously tried to compile the .java file in a CMD, close that CMD
and open it again as the changes made will take effect only using a new instance of CMD
Go to the location of the file.java , example:E:\Projects , right-click by holding the Shift button pressed inside your folder and in the options from the window that just appeared select: Open command window here
Another solution is to normally open a CMD and change the directory using :
cd command until you reach your folder
After the CMD window opened, type: javac HelloWorld.java
Note! Make sure the class name written in your file.java is the same as the file name. Example your file name should be: HelloWorld.java and your class inside that file must also be:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
Otherwise, it will not compile!
It is successful if no error message is displayed, if the path of the folder appears again in CMD and of course if the HelloWorld.class file appears now in the directory. To check that type the command dir which will show what contains your currect directory.
To finally run the file type in the CMD: java HelloWorld
Observe that no extension is needed when running the file(the file you run already has the .class extension)
This is how it worked for me! If something is not right, please inform me! Thanks!
For a beginner programmer that doesn't have admin rights on his/her computer, I'd recommend the Eclipse IDE.
Since you already have the JDK the only installation step needed requires no admin rights. From here you must download the "Eclipse Standard" option, and you will get a very large zip archive. You can extract it onto the desktop or my documents. Windows comes with a utility to do this via drag-and-drop right from the explorer or your machine may have another program such as WinRAR installed to do this.
You can then run eclipse.exe from the place where you extracted it by browsing to, and double-clicking this file.
The IDE is very powerful and self-explanatory. You can create projects, run, and debug code, and it's nice for beginners. It's truly worth the long wait in downloading it.
First, yes you did add JDK to PATH. But you didn't do that correctly. You already have these variables added to PATH: C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\
To add JDK put semicolon before you add it because you have multiple path's assigned to PATH variable. So it should be something like this:
C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.7.0_25\bin
in my case, javac was not working just because while specifying the path to javac in the path variable I gave space after putting semicolon to the end of the previous entry
To use javac from command prompt without typing the full location each time you will need to add it to the path.
I am unsure if you can change it without admin privileges, But on windows 7 with admin you can go to start -> Right click My Computer -> Properties -> Advanced system settings -> Advanced -> Enviromental Variables
You can then find the system variable path and append to the end of it, the location of javac.
The exact location of javac will vary depending on what version of the JDK you have installed, During installation you will have been given the option to choose where it was installed.
EDIT: Also make sure you haven't opened cmd as administrator. Or perform the steps in that video on the administrator account.
Adding variables to the top part of the environmental variables menu, mean they only affect the current user. You should be able to do these on normal account if you have someone type in the administrator password.
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");
This is definitely a very beginner question that might not have to do anything with programming. After few days, I have finally identified the problem as for why I couldn't sign jar using jarsigner. I kept getting this error that says "jarsigner: unable to create hello.jar.sig".
Basically, what I did was to put a jar in Java jdk's bin folder and then I shift-left click there in the folder to click on "Open the command window here". I generated the key from keytool and also used selfcert. Then when I did the jarsigner for a jar already put in the bin folder, I got the error. I eventually figured out the reason. I couldn't create the jar.sig in the bin folder since I don't have sort of permission. The question is, how do I set the destination to some folder that is modifiable?
I have no idea how to set the path to java directory and calling jarsigner from desktop or elsewhere, so I had to learn to write command prompt right inside the java bin folder.
Move your JAR file to a directory you can read/write.
Run the original command:
"C:\Path\To\JDK\bin\jarsigner" [options] jar-file
By default the output will be written to the directory you ran the command from.
In the long term, you can add the JDK bin directory to your Windows path.