Why does this javac command not work? - java

javac -cp .:gson-2.3.1.jar:commons-io-2.4.jar File.java
The above command works on one of my linux machines. However it does not work on another one even though it is the same distro! (Debian)
The shell does not throw any errors of any sort which suggests it is finding the .jar files just fine, however the java compiler throws errors wherever I have used the .jar files in my code e.g. "the import org.apache cannot be resolved" , "the import org.gson cannot be resolved" etc.
The Java file and both the required .jar files are in the current directory. I am using Java 1.6. What is going wrong here?

FIXED. Out of desperation I changed javac -cp to javac -classpath and it magically worked. Always pays to be explicit!

Related

Javac Compiles Program, but Can't Run with Java

I've never had issues with compiling with javac and running with java before, but here we are.
The program, MainApp.java, depends on a jar file and a few other programs, so I ran the following (which ran without issue):
javac -cp gson.jar *.java
However, when it comes time to run it, I can't seem to get it to work. I've tried:
java -cp gson.jar MainApp.java
but it complains that it can't find the other java files.
I then tried listing the file it said it was missing:
java -cp gson.jar RepositoryData.java MainApp.java
but it gave the same error.
I then tried listing all the dependencies:
java -cp gson.jar Comment.java Issue.java Owner.java Repository.java RepositoryData.java MainApp.java
but it complains that it can't find a main function in the Comment.java file - this leads me to think I'm going about this the wrong way, and I probably shouldn't be directly referencing dependencies in the java call.
I also know that the program does work, as I've compiled it using an editor without issue, but I'm required to do this via the command line for a class.
How should I be properly calling it?

Could not find or load main class Javac

Quick note, I went through every single other question through here and nothing seems to work.
So, here is my issue. All I want to do is create a windows batch script that will be used to execute my selenium project on Jenkins. Sounds simple right ? Its probably is, but I am missing something...
Here is my project https://github.com/Daviditooe/Nomad
First command I tried:
javac src/nomad/execute/Execute.java
Execute.java:9: error: package nomad.sites does not exist import nomad.sites.MmaShare;
It also couldnt find any of the jars, so I add all of them
Then I tried:
javac -cp Jars\* src\nomad\execute\Execute.java
This fixed the jars issue but the package not found still exists
So then I tried compiling every single package at the same time
javac -cp Jars\* src\nomad\execute\*.java src\nomad\actions\*.java src\nomad\baseactions\*.java src\nomad\browsers\Chrome.java src\nomad\directory\*.java src\nomad\scripts\*.java src\nomad\sites\*.java src\nomad\urltools\*.java
So, now its not crashing, then I tried compiling...
java src\nomad\execute\Execute
And its giving me Could not find or load main class
So That last thing I tried was compile all of them at the same time.
java src\nomad\actions\MmaShareActions src\nomad\baseactions\BaseActions src\nomad\browsers\Chrome src\nomad\directory\Directory src\nomad\execute\Execute src\nomad\scripts\Vpn src\nomad\sites\MmaShare src\nomad\urltools\UrlTools
Still no luck... anything thoughts would be appreciated.
Again: You must go into the src folder before you execute javac and java!
I can compile and execute it on my Linux Laptop:
stefan#stefanpc:/hdd/stefan/Downloads/Nomad-master/src$ javac -cp '../Jars/*' nomad/execute/*.java nomad/actions/*.java nomad/baseactions/*.java nomad/browsers/Chrome.java nomad/directory/*.java nomad/scripts/*.java nomad/sites/*.java nomad/urltools/*.java
Note: nomad/baseactions/BaseActions.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
stefan#stefanpc:/hdd/stefan/Downloads/Nomad-master/src$ java -cp '../Jars/*:.' nomad.execute.Execute
Exception in thread "main" java.lang.IllegalStateException: The driver executable does not exist: /hdd/stefan/Downloads/Nomad-master/src/C:\Users\Davidito\Desktop\AutoLinks\Jars\chromedriver.exe
Of course my Linux machine does not have the chromedriver.exe installed.
Under Windows, you must use "\" instead of "/" and you must use ";" instead of ":". Note that you must add the current folder "." to the class path when you execute the program. Otherwise java would only search in the Jars folder.

How to use javac with jars and packages

I'm trying to run java from the command line, and haven't had to include extra packages with the javac command before and I can't figure out what I'm doing wrong.
I'm running javac -d bin -cp jar1:jar2:...:jarN:PackageName MyClass.java but I'm still getting an error: package PackageName does not exist
I'm using the absolute paths for everything, and I also tried individually listing the java files inside the package but that didn't work either. I'm using a colon since I'm on a mac.
Anyone know what I am doing wrong? Thanks for any help!
I realized that I needed to compile the java files in PackageName before I could compile MyClass.java, which depended on them. So what I needed to do was: javac -d bin -cp jar1:jar2:...:jarN PackageName/*.java and then I could compile MyClass.java with bin added to the classpath as well as the jars.

Using Command Prompt to execute a java file

The problem I'm currently having is that I am trying to execute a java file using command prompt, I understand the PATH being set to the jdk's file. Though my java file contains libraries and has to import the libraries, how would I ' import the libraries ' when it runs?
Sample command :
javac ClassName.java 1 1 1
When it executes it errors on the imports so what should I do?
The .jar files are Java "libraries". What you need is something like:
> javac ClassName.java
> java -cp Library1.jar:Library2.jar ClassName.class
The first line (javac) compiles the Java code into a class file. The second line runs the compiled class. The '-cp' option sets the CLASSPATH (makes the code in the jar files available at runtime). Note: the exact syntax will depend on if you are using Mac OSX/Linux or Windows. Windows uses the ';' character to separate the jar file names.
Several issues here:
Are you compiling or executing? javac is the compiler. java is the runtime virtual machine.
ClassName.java is source. ClassName.class is the compiled bytecode run by java.
As a general rule, your environment should contain the variable JAVA_HOME, and that should point at the directory where the JDK resides on your computer.

Running Java from command. Java errors with -cp and without (JDBC error / Class not found)

I am trying to compile and run a program from the command line. When we compile it, we do -cp and link it to our jdbc jar.
javac -cp jdbc.jar *.java
If we then just do:
java debugger
We then get an error saying that the driver can't be found.
However, if we do:
java -cp jdbc.jar debugger
Then we get an error saying that the class Debugger can't be found.
Which way do we need to do it? Since neither is working, any suggestions on what we might need to check to fix the correct method?
Many thanks from a tired group of students!
You also need to tell the java command where the javac command left the .class files. Perhaps
java -cp .:jdbc.jar Debugger
would do the job in your case?
I believe it should be -classpath not -cp.
Please check the javac documentation and also here

Categories

Resources