I have a java class I'm trying to run that references various other jar files. It looks something like this:
package com.myapp.test;
import java.io.File;
import com.myapp.ref;
import com.myapp.stuff;
import com.strangersapp.stuff;
import com.strangersapp.morestuff;
public class myTest {
public static void main...
...
}
The com.myapp.* classes are in another jar file called myapp.jar. The strangersapp classes are in a strangersapp.jar. There are several other referenced classes in various jars. My whole directory structure looks like this:
myapp.jar
strangersapp.jar
someother.jar
yetanother.jar
etc.jar
com\myapp\test\myTest.java
My problem is trying to compile and run this. I try the obvious but this command does NOT work. It doesnt pickup all the jar files:
javac -cp . com\myapp\test\myTest.java
java -cp . com.myapp.test.myTest
This command also does not work:
javac -cp ".;*.jar" com\myapp\test\myTest.java
However, these commands do work:
javac -cp ".;myapp.jar;strangersapp.jar;someother.jar;yetanother.jar;etc.jar" com\myapp\test\mTest.java
java -cp ".;myapp.jar;strangersapp.jar;someother.jar;yetanother.jar;etc.jar" com.myapp.test.mTest
Would anyone know why my first java run statement is not working?? I dont want to type all the jar names out in the classpath reference...
You should try this:
javac -cp ".;*.jar" com\myapp\test\myTest.java
java -cp ".;*.jar" com.myapp.test.myTest
If you use -cp you have to name all the jars you want to include. To avoid that just add them to your manifest and java will pick them up.
For javac i think you are supposed to use -classpath rather than -cp
Related
When I try to execute a java file within a package, the integrated terminal can't find the class file. How should I configure vscode or the command to automatically add the packages on the command like java package.File or search the class file automatically? I'm using Code Runner extension with this command: "java": "cd $dir && javac -d \"$workspaceRoot/java/bin/\" $fileName && cd \"$workspaceRoot/java/bin/\" && java $fileNameWithoutExt. This works when the java file isn't in a package.
You need to have java run as <package>.ClassName. For example, if your class looks like this:
package com.something;
class Something {
public static void main(String[] args) {
System.out.println("Hello World");
}
};
And you run javac with the following:
javac -d . Something.java
Then, you need to make sure that java knows which package you want to run:
java com.something.Something;
In other words, you need to explicitly tell java what package your main class is in.
Basically, you need to create another variable inside of vscode that allows you to add the package to the beginning of the command:
"java": "cd $dir && javac -d \"$workspaceRoot/java/bin/\" $fileName && cd \"$workspaceRoot/java/bin/\" && java $filePackage.$fileNameWithoutExt
Unfortunately, it doesn't look like https://github.com/formulahendry/vscode-code-runner has a java package variable already defined, so it's something you'd have to either create by downloading the source of the package and hacking it, or requesting a variable from the package author at https://github.com/formulahendry/vscode-code-runner/issues
Update:
It looks like this is already an issue in the CodeRunner package: https://github.com/formulahendry/vscode-code-runner/issues/118. You might want to watch this issue for updates and upgrade your extension when it is fixed.
As a temporary workaround, I think just not specifying a package for your main class will work. All other classes are going to be using import anyway, so it doesn't matter for those, but keep your main class in the top-level package and VSCode Code Runner should work as expected.
Hello so recently I have started to transfer from c++ to java and one exercise is to compile and run a java program using cmd.
So okay, I coded my simple HelloWorld program using netbeans and saved it,
package helloworld;
public class Helloworld
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
so now my saved .java file is in C:\Users\eatmybuns\Documents\NetBeansProjects\Helloworld\src\helloworld
now I open the cmd and I change the directory to the above and typed
javac Helloworld.java
and now I can see Helloworld.class in the same folder, I read from somewhere that I have to include the package name as well for it to run so I typed
java helloworld.Helloworld
it gave me an error so I tried running it from the src folder instead but it also gave me the same error.
Error: Could not find or load main class Helloworld
Caused by: java.lang.ClassNotFoundException: Helloworld
I have read some possible solutions such as using -cp or using -d but it keeps giving me the same error. I am currently using jdk1.8.0_161. on windows 10.
You have to use
java helloworld.Helloworld
and from the parentfolder of helloworld, which is the src directory, in your case.
There is a tight relationship between package and directory structure.
There are many flags you can set for the compiler, like srcdir, targetdir to keep classes and sources apart. But basically, when you invoke your class helloworld.Helloworld, the JVM looks for a directory helloworld/ and expects a Helloworld.class there.
To achive this without compiler flags, you have to put the source into the helloworld/ folder too.
The whole name of your class is helloworld.Helloworld and java should look there and find it there.
mkdir helloworld
mv Helloworld.java helloworld/
javac helloworld/Helloworld.java
java helloworld.Helloworld
Hello world
It's a bit surprising in the beginning, if you don't know it and started with classes without package declaration. But the logic is simple and straight forward: Every package is reflected by the directory structure.
With a distinction of sourcedir and targetdir, the directory structure below has to be the same as without, just the starting point differs. Common target dirs are classes or bin, like in:
javac -s . helloworld/Helloworld.java -d classes
or
javac -s ./src helloworld/Helloworld.java -d bin
But bin or classes don't get part of the package name, and you can't extend the invocation of the class by prepending that dir to the invocation path:
java bin.helloworld.Helloworld
won't work. But
java -cp ./bin helloworld.Helloworld
should. If you read the documentation carefully, you will find, that it carefully distinguishes source file (Helloworld.java), class (Helloworld) and file (Helloworld.class).
I have 2 classes, each is on different folders
Main class src/mainclass/Main.java
Display class src/swingclass/Display.java
Main Class:
package src.mainclass;
import src.swingclass.Display;
public class Main{
public static void main(String[] args){
Display sM = new Display();
}
}
Display Class:
package src.swingclass;
public class Display{
public Display(){
System.out.println("Display class");
}
}
i run it on cmd and it is stated
error: package src.swingclass does not exist
and also, Main class could not see Display class.
overall, i have 3 errors.
I know that using IDE will make this easier, but i want to learn this on cmd. I followed the tutorials on net, but it still has errors.
I believe you should get rid of the src part of the package, i.e.
package swingclass;
...
and import it like:
import swingclass.Display;
Remove all occurrences of "src." in all files
Change to the src directory
Type: javac mainclass/Main. java
Type: javac swingclass/Display.java
Type: java mainclass.Main
Go to the src folder level (go inside src folder) in your command prompt, and use the commands
javac -classpath . src/mainclass/Main.java
to compile (this will implicitly compile the Display class as well, since it is required by Main class) and
java -classpath . src/mainclass/Main
to run the Main class.
. indicates the current directory (in your case, src folder) relative to which the other paths (to source/class files) are then specified.
javac -d bin -sourcepath src <path to main class from src directory, in your case it will be one folder up from src/mainclass/Main.java>
java -cp bin src.mainclass.Main (set class path to bin root where class files are, scr is where your package starts)
Using the -classpath flag should help you solve your problem.
javac -d "theClassPathNameYouWant" "src/swingclass/Display.java"
javac -d "theClassPathNameYouWant" "src/swingclass/Main.java"
java -classpath "theClassPathNameYouWant" swingclass.Main
If you have a jar file called myJar.jar located in /myfolder and you want to use the class called myClass from it, how do you go about doing it from the command line?
I thought it would be to go into the directory and say java -cp myJar.jar.myClass but that isn't working. Any help would be appreciated.
Use java -cp myjar.jar com.mypackage.myClass.
If the class is not in a package then simply java -cp myjar.jar myClass.
If you are not within the directory where myJar.jar is located, then you can do:
On Unix or Linux platforms:
java -cp /location_of_jar/myjar.jar com.mypackage.myClass
On Windows:
java -cp c:\location_of_jar\myjar.jar com.mypackage.myClass
You want:
java -cp myJar.jar myClass
The Documentation gives the following example:
C:> java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool
There are two types of JAR files available in Java:
Runnable/Executable jar file which contains manifest file.
To run a Runnable jar you can use java -jar fileName.jar or java -jar -classpath abc.jar fileName.jar
Simple jar file that does not contain a manifest file so you simply run your main class by giving its path java -cp ./fileName.jar MainClass
Assuming you are in the directory where myJar.jar file is and that myClass has a public static void main() method on it:
You use the following command line:
java -cp ./myJar.jar myClass
Where:
myJar.jar is in the current path, note that . isn't in the current path on most systems. A fully qualified path is preferred here as well.
myClass is a fully qualified package path to the class, the example assumes that myClass is in the default package which is bad practice, if it is in a nested package it would be com.mycompany.mycode.myClass.
This is the right way to execute a .jar, and whatever one class in that .jar should have main() and the following are the parameters to it :
java -DLB="uk" -DType="CLIENT_IND" -jar com.fbi.rrm.rrm-batchy-1.5.jar
trying to run this program. I think that to setup all of the web service stuff I need to run apt. (Although using javac I am having the same issue). I think what I am getting is compile errors. (Shown at bottom).
I think what I need to do is include this jar in my class path: jsr181-api.jar (source). Is there a simple temporary way to do this (on solaris)? I don't want to add it to my bash_rc file (it is there forever). I also know that there is some way to do it using a manifest text file but that seemed complicated so I didn't look into it yet. Can I just do something like:
javac HelloImp <listOfJars>
or
ant HelloImp <listOfJars>
Code:
package server;
import javax.jws.WebService;
#WebService
public class HelloImpl {
/**
* #param name
* #return Say hello to the person.
*/
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
Compile errors:
HelloImpl.java:3: package javax.jws does not exist
import javax.jws.WebService;
^
HelloImpl.java:5: cannot find symbol
symbol: class WebService
#WebService
^
2 errors
Update: Cool that is wrapped up but it is still not quite working. I have created a new question to keep things nice and organized:
Try the following:
java -cp jar1:jar2:jar3:dir1:. HelloWorld
The default classpath (unless there is a CLASSPATH environment variable) is the current directory so if you redefine it, make sure you're adding the current directory (.) to the classpath as I have done.
In windows:
java -cp C:/.../jardir1/*;C:/.../jardir2/* class_with_main_method
make sure that the class with the main function is in one of the included jars
Note for Windows users, the jars should be separated by ; and not :.
for example:
javac -cp external_libs\lib1.jar;other\lib2.jar;
Use the -cp or -classpath switch.
$ java -help
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
...
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
(Note that the separator used to separate entries on the classpath differs between OSes, on my Windows machine it is ;, in *nix it is usually :.)
javac HelloWorld.java -classpath ./javax.jar , assuming javax is in current folder, and compile target is "HelloWorld.java", and you can compile without a main method