Compile java with tomcat servlet-api.jar using Powershell - java

I am currently following this tutorial on tomcat, Step 6(b) Compiling the Servlet. I was able to compile in cmd, but I want to try to get it to work in Powershell as well.
cmd:
javac -cp .;c:\myProject\tomcat\lib\servlet-api.jar HelloServlet.java
How would I compile using Powershell?
I saw this somewhere, but this isn't compiling the java source file into a class:
java -cp '{jar path}' -Dcontext=atest1 -Dresourcepath=." DW_Install
I'm fairly novice at powershell, so I don't really understand if I need -Dcontext and -Dresourcepath. What does -D do?

I imagine the problem is the semi colon. In PowerShell, the semi colon is an end of statement character, so the above command will be treated as if it were two separate commands:
javac -cp .
c:\myProject\tomcat\lib\servlet-api.jar HelloServlet.java
To fix this, I imagine you'll need to quote the class path argument:
javac -cp '.;c:\myProject\tomcat\lib\servlet-api.jar' HelloServlet.java

Related

Run a Java Class with Command-Line

I have 4 Java-Data:
Common.java
Constants.java
KeywordsEditor.java
ExecutionEngine.java (There is here a Main-Method)
I have successful compiled in Command-Line with this Command from Project-Directory (C:\ProjectDemo\src\main\java\ValueInput)
javac -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar" *.java
I got 4 Data .class in the same Directory. Now i want to run them with this code:
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar" ExecutionEngine
But i got Error:
Error: ExecutionEngine main class could not be found or loaded
```
I've tried with some same code else:
```
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;libs\*;selenium-server-standalone-3.141.59.j‌​ar" ExecutionEngine
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;libs/*;selenium-server-standalone-3.141.59.j‌​ar" ExecutionEngine
```
And some more, but they don't work. Can somebody help me?
Update
From your comment, I learnt that you have package ValueInput; mentioned in ExecutionEngine.java. Therefore, you should use the switch -d when compiling:
javac -d . -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar" *.java
The option -d . asks the compiler to place the generated class files at the current directory. Now, if you use the command ls in Mac/Unix or dir in Windows, you will see a directory, ValueInput has been created and all the .class files have been placed inside this directory. Learn more about the switches by simply using the command javac
In order to execute ExecutionEngine.class, you can now use the following command:
java -cp ".;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar" ValueInput.ExecutionEngine
You can also check this answer for a similar solution.
Side note: You should follow the Java naming conventions. As per the convention, the name of the package should be something like value.input.
Original answer
The root cause of the problem is using only jars with -cp. You missed realizing that your ExecutionEngine.class is not in the jars; rather it is at the current directory which is denoted by a dot (.) which you missed to include in the classpath.
Thus, the correct command will be:
java -cp ".;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar" ExecutionEngine
It doesn't matter where you put . i.e. the current directory e.g. the following will also work for you:
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar;." ExecutionEngine
Note for Mac:
The separator used for this purpose in Mac is : instead of ; e.g.
javac -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
java -cp mysql-connector-java-5.1.49.jar:. MysqlDemo
Note for Java-11 onwards:
Java-11 allows launching Single-File Source-Code programs without compiling e.g.
java -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
You can learn more about it from this article.

How to import external jars in command line or Atom?

I have been using IntelliJ to run my java programs that require some external jars. I also learned that if I want to compile and run my program from the command line I should do the following:
java -classpath someJar.jar YourMainClass
or for many libraries:
java -classpath someJar.jar;myJar.jar YourMainClass
However, while placed in the src folder where my class it doesn't seem to find my class.
I also like using the Atom text editor but I don't know any package that can import external libraries like an IDE does. So how do I do it in Atom or in cmd in Windows 10? I am kind of a newbie to java dev outisde my beloved IDE, so I would really appreciate some help.
If you're on a Windows system then try compiling (with the jar) using the following command in cmd:
javac -cp .;jar-name.jar *.java
To run the command with the jar use:
java -cp .;jar-name.jar JavaCodeName
If you're on a Unix system then you can try the following to compile in terminal:
javac -cp jar-name.jar:. *.java
And to run it use:
java -cp jar-name.jar:. JavaCodeName
I'm not too familiar with Atom so I don't know if there is an attachment to do this, but it should work for terminal / command prompt.

compiling java program with multiple .jar files (mac)

I am currently trying to write a program that reads the metadata of images using the library from here: https://github.com/drewnoakes/metadata-extractor/wiki/GettingStarted. My issue is that I cannot figure out how to compile the program using more than one .jar file (and it requires two). Both jar files are in my working directory, with the java file I'm trying to compile.
This is the command I am using, with just one .jar file referenced.
javac -cp metadata-extractor-2.7.2.jar MetadataPhotoExtractor.java
Thanks for any help
-Aaron
Use colon on unix-like systems as separator.
javac -cp metadata-extractor-2.7.2.jar:my-other-jar.jar MetadataPhotoExtractor.java
On Windows use semicolons because the colon is restricted for drive letter separation.
for windows
javac -cp metadata-extractor-2.7.2.jar;myother.jar MetadataPhotoExtractor.java
for Linux
javac -cp metadata-extractor-2.7.2.jar:myother.jar MetadataPhotoExtractor.java
More arguments are accountet as a list separated with : of the current parameter.
javac -cp lib1.jar:lib2.jar:lib3.jar myClass.java
Have you tried:
javac -cp metadata-extractor-2.7.2.jar:xmpcore-5.1.2.jar MetadataPhotoExtractor.java

How to add Jar files in Java in Linux

I have a Java code where I am importing Guava packages. I run it in windows command prompt using following commands:
javac -cp guava-11.0.2.jar Test.java
java -cp guava-11.0.2.jar;. Test
However, in Linux it is giving error. Can anybody help me to solve this issue.
The path separator on Linux/Unix is a colon, i.e. :.
So in your case the second command on Linux/Unix would be:
java -cp guava-11.0.2.jar:. Test
class path entries are separated by colons in Linux (not semicolons as in Windows)
Try that:
java -cp guava-11.0.2.jar:. Test

Execute Java from command line

I have a folder on my desktop titled "Stuff" and in that folder I have the following:
Hello.java
mail.jar
And Hello.java imports from mail.jar, so I need to tell Hello.java to look for mail.jar.
From a Windows command line and from a unix command line, how can I compile this and run this?
Compile:
javac -cp .;mail.jar Hello.java
where ; is for Windows; use : for *nix.
and run:
java -cp .;mail.jar Hello
where again, use ; for Windows and : for *nix.
-cp tells both javac and java what classpath to use, and as your files are in the local directory where you're executing the command, you can use . for the Hello part and the name of the jar for the paths inside the jar. Wikipedia has a decent article on classpaths.
Mind you, if you're going to be doing this on a regular basis, you may want to set your CLASSPATH environment variable rather than constantly using the -cp flag. Both java and javac use the CLASSPATH variable.
For my own development machine, I actually include . in my CLASSPATH variable, for convenience. It's not something I would do on a production or build/test box, but it's very handy for development purposes. You'd want to have your usual jars in it as well.
Assuming Hello.java does not contain a package declaration, on Windows:
javac -cp mail.jar Hello.java
java -cp mail.jar;. Hello
The only difference on Unix platforms is that you separate the elements of the classpath with a scolon instead of a semicolon:
java -cp mail.jar:. Hello
Follow this tutorial and you should be able to do it in no time:
Java Compilation
You also shouldn't have any problems with the classpath because your classes are in the same folder

Categories