I'm trying to run java and javac from the command line, but I'm having trouble setting the CLASSPATH for the javac sdk tool.
I've successfully added the CLASSPATH variable through the windows environmental variable settings, and this works for the java command. I am able to execute class files from any directory in the command line. However, when trying to use the javac command (where the .java files are in the same CLASSPATH directory), i receive the error message that the file is not found.
My CLASSPATH variable is set to:
C:\Users\ejovo\OneDrive\Documents\Coding\Java>
Here are 3 examples of what I mean.
C:\>javac MyFirstApp.java
javac: file not found: MyFirstApp.java
Usage: javac <options> <source files>
use -help for a list of possible options
C:\Users\ejovo\OneDrive\Documents\Coding\Java>javac MyFirstApp.java
C:\>java MyFirstApp
Hello World
We see that the java command can be run from anywhere while the javac still has to be run from the directory in which the .java files are.
I've tried setting the javac CLASSPATH with the -cp and -classpath options without any luck:
C:\>javac -cp C:\Users\ejovo\OneDrive\Documents\Coding\Java
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options
C:\>javac -classpath C:\Users\ejovo\OneDrive\Documents\Coding\Java
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options
And I've already set a PATH variable that makes the java and javac commands run correctly
I've also tried changing the sourcepath with the -sourcepath argument:
C:\>javac -sourcepath C:\Users\ejovo\OneDrive\Documents\Coding\Java
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options
Despite that, I clearly do have a .java source file present:
Directory of C:\Users\ejovo\OneDrive\Documents\Coding\Java
07/25/2019 03:08 PM <DIR> .
07/25/2019 03:08 PM <DIR> ..
07/25/2019 03:29 PM 425 MyFirstApp.class
07/24/2019 06:40 PM 127 MyFirstApp.java
2 File(s) 552 bytes
2 Dir(s) 57,735,630,848 bytes free
Let me know if anyone has any other ideas!
It appears that you are getting a bit confused here. The CLASSPATH is used to tell the java and javac programs where to find compiled .class and .jar files.
You are attempting to use the CLASSPATH to have javac locate SOURCE files, which does not work.
See here:
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
You might wish to examine the -sourcepath argument.
Related
I have a source file Example.java in the following location:
C:\Users\sushr\Desktop\Experimental Java code\tutorial
Result of dir command from tutorial directory:
Directory of C:\Users\sushr\Desktop\Experimental Java code\tutorial
10/10/2020 01:51 PM <DIR> .
10/10/2020 01:51 PM <DIR> ..
10/10/2020 01:51 PM 133 Example.java <- This is the source file
I am trying to compile this file from location C:\ .
The command that I am running from the command prompt is the following:
C:\>javac -cp "C:\Users\sushr\Desktop\Experimental Java code\tutorial" Example.java
I am getting the following error:
error: file not found: Example.java
Usage: javac <options> <source files>
use --help for a list of possible options
The classpath setting for javac is for finding other libraries and classes while compiling your .java files. It is not used for finding the .java files you specified as argument for the javac program. When you call javac Example.java and you are currently in the directory C:\, then it will look for a file C:\Example.java. It is most likely that the Example.java file will not be directly in the file system root C:\.
Either specify the .java files with an absolute path or adjust your working directory with cd "C:\Users\sushr\Desktop\Experimental Java code\tutorial\" to go in that directory and compile the files from that location.
If you specify the absolute path to your .java file you should be able to just compile it without the -cp flag like so:
C:>javac "C:\Users\sushr\Desktop\Experimental Java code\tutorial\Example.java"
In macOS, I have noticed that using the "~/" to shortcut to home, it does not work. For example:
javac -cp .:~/algs4/algs4.jar MyFile.java
Instead, I had to use the full path to locate the .jar file in order to compile:
javac -cp .:/Users/username/algs4/algs4.jar MyFile.java
I was doing WordCount using Ubuntu Shell, when I was Compile the java code by using the following command as mentioned below
javac -classpath ${HADOOP_CLASSPATH} -d
‘/home/abdullah/Desktop/WordCountTutorial/tutorial_classes’
‘/home/abdullah/Desktop/WordCountTutorial/WordCount.java’
I got the error below
javac invalid flag:‘/home/abdullah/Desktop/WordCountTutorial/tutorial_classes’
‘/home/abdullah/Desktop/WordCountTutorial/WordCount.java’
Usage: javac <options> <source files>
use -help for a list of possible options
I can observe following issues:
Quotes: Don't give quotes. -d expects a directory path.
direct directory path : should be changed with a java file path or use **/*.java e.g. /home/abdullah/Desktop/WordCountTutorial/tutorial_classes/*.java
Getting errors in Ubuntu while running command ./run1.sh
javac: file not found: edge.java
Usage: javac <options> <source files>
use -help for a list of possible options
Error: Could not find or load main class median_degree
In run1.sh file
javac -cp .:libs/java-json.jar:./src edge.java ./src median_degree.java
java -cp .:libs/java-json.jar:./src median_degree
File Structure
Can anyone help on how to write script file for this? I have tried lots options but none of them is working. Thank you
Try this:
javac -cp .:libs/java-json.jar ./src/edge.java ./src/median_degree.java
as the source files are not part of the class path and needs to be supplied as source files using the full path.
I am using javac to compile a file called Rengine.jar.
I tried:
javac –classpath ./REngine.jar
javac –cp ./REngine.jar
javac ./REngine.jar
javac –classpath REngine.jar
And here are the errors that I got:
javac: invalid flag: –classpath
Usage: javac <options> <source files>
use -help for a list of possible options
javac: invalid flag: –cp
Usage: javac <options> <source files>
use -help for a list of possible options
javac: invalid flag: ./REngine.jar
Usage: javac <options> <source files>
use -help for a list of possible options
javac: invalid flag: –classpath
Usage: javac <options> <source files>
use -help for a list of possible options
I am not familiar with Java at all and I could not find an answer to this problem with a Google search. I precise that I am in the good directory and that I am using Mac OSX. My Java version is 1.8.0_20.
Any advice would be much appreciated! :)
Firstly, you cannot compile a .jar file. A .jar file is a collection of complied .java files (.class files). You are however able to run a .jar file if your manifest file is set up correctly.
The procedure is as follws:
Compile:
javac MyClass.java
the above line creates MyClass.class
Create JAR with manifest file:
jar cfe myJar.jar myClass myClass.class
Run JAR:
java -jar myJar.jar
javac - the Java source compiler, will compile the Java source file *.java into JVM bytecode the class file *.class
To execute a Java program you need to call a class file with java YourClass.
The Jar file is a Java archive file. Which contains other files. For example class files. If you want to run a class from within the Jar file there a two way.
executable Jar
java –jar REngine.jar
This depends on the manifest file inside the Jar, which defines the main class which should be executed.
class inside a Jar file
java –cp REngine.jar package.YourClass
This would execute the class package.YourClass (assuming the class has a main method).
specify the option and source file, try like this:
javac -classpath ./lib/* file.java
in case you have any problem try specifying which artifact the compiler will use:
javac -classpath ./lib/myjar.jar file.java
i run command
javac -classpath /home/coolhunk/JBoss/jboss-6.0.0.Final/common/lib/jboss-servlet-api_3.0_spec.jar -d helloapp.war/WEB-INF/classes -sourcepath src/com/manning/jbia/intro/*
for generating java class files.but i am getting error
javac: invalid flag: src/com/manning/jbia/intro/HelloWorldServlet.java~
Usage: javac <options> <source files>
use -help for a list of possible options
can anyone please point out what is the mistake in this command ??
Try these commands using tomcat, place your servlet source in src folder and run these,
C:\Documents and Settings\ssit>cd C:\src
C:\src>javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 6.0\
lib\servlet-api.jar" MyServlet.java
you can get the class file for the servlet. After getting the class file make the war file.
javac -classpath /home/coolhunk/JBoss/jboss-6.0.0.Final/common/lib/jboss-servlet-api_3.0_spec.jar -d helloapp.war/WEB-INF/classes -sourcepath src/com/manning/jbia/intro/*
The problem is the last item. The wildcard causes it to be expanded into everything in the directory, which causes everything after the first expansion to be treated as a source file name. The expansions also appear to include src/com/manning/jbia/intro/HelloWorldServlet.java~, which the compiler doesn't want to know about.
Try this:
javac -classpath /home/coolhunk/JBoss/jboss-6.0.0.Final/common/lib/jboss-servlet-api_3.0_spec.jar -d helloapp.war/WEB-INF/classes -sourcepath src src/com/manning/jbia/intro/*.java