I have a project that I have compiled to a jar with Maven which I named H.jar.
The maven command I use is (in eclipse):
maven install
The jar file has a class called Person in it.
I have added the jar file to the classpath on a Windows machine.
echo %classpath%
Results -> C:\location_to_jar\H.jar
But when I try to compile the program I get error:
error. Cannot find symbol.
I am running the command:
javac Main.java
The class looks like this:
public class Main {
public static void main(String[] args) {
Person p = new Person("John", "Doe");
p.toString();
}
}
Should the program not just find the class if I have added it to the classpath?
It sounds like your program is fully contained within a single jar, with no external dependencies, so I would recommend that you avoid using the %CLASSPATH% environment variable. Instead, point Java at the jar directly.
Assuming that your Maven build has generated an executable jar with your Main class defined as its entry point then you can just execute:
java -jar C:\location_to_jar\H.jar
If the executable class has not been defined in your build, then you can define the classpath on the command line:
java -cp C:\location_to_jar\H.jar com.mypackage.Main
You will need to replace the package in the command above with the appropriate package name as defined in your application.
If you want Maven to make your jar executable, then you can use the Maven Assembly Plugin to help. An answer which explains that can be found here: How can I create an executable JAR with dependencies using Maven?
Related
I know this question has been asked a lot, but I have tried a few suggestions and am still getting this error.
I am running the jar as follows:
java -jar MyJar-1.0.jar com.me.ldap.ActiveMain,
where my ActiveMain.java file looks like this:
package com.me.ldap;
public class ActiveMain {
public static void main(String[] args) throws Exception {
...
}
}
I have also tried simply java -jar MyJar-1.0.jar with the same Error: Could not find or load main class error. I've also looked into the class path option but I don't think that applies.
I am creating it in Intellij as a Maven project. Maven > Lifecycle > package.
In order for:
java -jar myfile.jar
to work, there must be a manifest file in the jar file that points to a main class.
In order for you to specify the main class on the command line, you need to specify a classpath, not a jar file. Like:
java -cp myfile.jar com.me.ldap.ActiveMain
You are conflating these two things. Either create a manifest that specifies your main class and use the -jar switch, or simply use the -cp switch and specify your main class on the command line.
Have you tried removing the signature files? I have seen this error pop up sometimes when due to signature files as documented in "Invalid signature file" when attempting to run a .jar and Generated JAR throws ClassNotFoundException for main class is also in the similar vein.
Java and Gradle beginner's question.
I made a project directory for java and gradle test:
The directory hierarchy :
HelloWorld.java:
package foo.bar;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
build.gradle:
apply plugin:'java'
Then,gradle build this project and generated what i need.
As you see above, my problem is why doesn't this execute correctly? Even through I cd to .class path.
======================================================================
While, if I remove package foo.bar; in HelloWorld.java, and repeat gradle commands and execute at he.bak directory then the error remained the same.
But when I cd to the directory where HelloWorld.java placed. everything goes OK!Why? something related with CLASSPATH environment variables or other causes?
////////////////////////////////////////////////////////////////////
UPDATE
////////////////////////////////////////////////////////////////////
Thought you guys' warm replies, I know that I should combine the CLASSPATH and the period-separated executable .class file to figure out what's going on when executing java class file.
I experiment my thought resulting in 2 point to this question:
The -cp option path parameter A/B plus the executable file c.d.e.class finally form the A/B/c.d.e.class full path where the class is actually located.
If I specify the package in source code file with package d,I must split the full path in the form of java -cp A/B/c/d e.class. split in other ways all will result in errors.
something I am not sure here is :
When I specify my package path in my source code file, It determined the only classpath when executing corresponding executable, right?
If it is the truth, How does a project with lots of package and sources files work?
What's the root principle?
When in build/classes/main try java foo.bar.HelloWorld instead of java HelloWorld
The reason you need to specify foo.bar.HelloWorld is because you specified package foo.bar;. This tells java that the class should be in foo/bar/HelloWorld and the fully qualified name for HelloWorld is foo.bar.HelloWorld. If you want to execute the class from a different working directory however, you can specify the classpath explicitly using the -cp option, e.g., java -cp c:\myproject\build\classes\main foo.bar.HelloWorld.
By the way, the classpath default is the current working directory (i.e., .) but java -cp c:\myproject\build\classes\main foo.bar.HelloWorld will NOT have the classpath set to the current working directory if it is explicitly set using the -cp option. If you want to include the current working directory but explicitly set it, or even add more directories, you can chain them using semicolons like this: java -cp .;c:\myproject\build\classes\main foo.bar.HelloWorld. So this will include both the current working directory and the directory I specified.
I use a jar file called korat.jar. I executed with the command line:
java -cp $CLASSPATH korat.Korat --visualize --class test.Add --args 3
The classpath contains the path of the jar and also the Add.class file.
I want to execute this jar in my own program java in netbeans IDE. I think I would use:
String []s={test.Add.class.getName(),"--visualize","--class","test.Add","--args","3"};
Korat.main(s);
I get this exception: java.lang.NoClassDefFoundError
What do you mean "to execute the jar in my own program"? The jar contains some classes, if they are in your class path, you can instantiate the classes themselves and invoke some methods. In that case, you should use test.Add class. But it seems like the class is not in your classpath - java.lang.NoClassDefFoundError.
I need help including imported jar files into my java program in Linux. Here is the program:
import java.sql.*;
public class CreateCoffees
{
public static void main(String args[])
{
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
System.exit(1);
}
}
}
In order to execute Class.forName("com.ibm.db2.jcc.DB2Driver"); I need two .jar files added into the classpath:
db2jcc_license_cu.jar
db2jcc4.jar
I put these jar files into the same directory as my CreateCoffees.java file, then compile and run it like this:
javac CreateCoffees.java
java CreateCoffees
But I got this error
ClassNotFoundException: com.ibm.db2.jcc.DB2Driver
Then I tried the "-classpath" option
javac -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar CreateCoffees.java
java -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar CreateCoffees
but got this
Exception in thread "main" java.lang.NoClassDefFoundError: CreateCoffees
Caused by: java.lang.ClassNotFoundException: CreateCoffees
How to I include those jar files into a my runnable jar so I can run it with java -jar myjar.jar ?
Try this
java -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar:. CreateCoffees
when you use -classpath it looses current directory from classpath so it needs . in classpath as well explicitly
How to include the jars of your project into your runnable jar:
I'll walk you through it step by step with Eclipse Version: 3.7.2 running on Ubuntu 12.10. I'll also show you how to make the build.xml so you can do the ant jar from command line and create your jar with other imported jars extracted into it.
Basically you ask Eclipse to construct the build.xml that imports your libraries into your jar for you.
Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class: Runner Put this code in there.
Now include the mysql-connector-java-5.1.28-bin.jar from Oracle which enables us to write Java to connect to the MySQL database. Do this by right clicking the project -> properties -> java build path -> Add External Jar -> pick mysql-connector-java-5.1.28-bin.jar.
Run the program within eclipse, it should run, and tell you that the username/password is invalid which means Eclipse is properly configured with the jar.
In Eclipse go to File -> Export -> Java -> Runnable Jar File. You will see this dialog:
Make sure to set up the 'save as ant script' checkbox. That is what makes it so you can use the commandline to do an ant jar later.
Then go to the terminal and look at the ant script:
So you see, I ran the jar and it didn't error out because it found the included mysql-connector-java-5.1.28-bin.jar embedded inside Hello.jar.
Look inside Hello.jar: vi Hello.jar and you will see many references to com/mysql/jdbc/stuff.class
To do ant jar on the commandline to do all this automatically: Rename buildant.xml to build.xml, and change the target name from create_run_jar to jar.
Then, from within MyProject you type ant jar and boom. You've got your jar inside MyProject. And you can invoke it using java -jar Hello.jar and it all works.
How can I create the class file and jar file for this coding, when I compile this program its not working because there is no main function in the program. And also I am trying in command prompt but I don't know how to set the classpath? please help me
My Coding is here
public class NewLogFields implements ILogNotify
{
public void onLog(Level level, String comment, IMediaStream stream, String category,String event, int status, String context) {
if (category.equals(WMSLoggerIDs.CAT_session) && event.equals(WMSLoggerIDs.EVT_destroy))
{
Long csBytes = (Long)WMSLoggerFactory.getGlobalLogValue(WMSLogger IDs.FD_cs_bytes);
Long scBytes = (Long)WMSLoggerFactory.getGlobalLogValue(WMSLogger IDs.FD_sc_bytes);
System.out.println("disconnect: csBytes:"+csBytes+" scBytes:"+scBytes);
}
}
}
Once you compiled the coding in wowza media Serever the jar file is automatically created in the library folder,see your Installation Library folder.Still you have problem Gothrough this link Wowza Quick Guide
What do you want to do?
Create a class and an jar file out of this Java code so that you can use this in another Java program?
Then you have to compile it:
java NewLogFields.java
Looks like you are unable to compile it at all. This could be because the interface ILogNotify (or the jar that contains this) is not in the classpath.
You can include the path/jar containing this interface in the classpath by using:
javac -cp .;path_to_jar_or_class NewLogFields.java
where path_to_jar_or_class is the path to the folder or jar file that contains ILogNotify.
For example, this may be something like ./logNotify.jar or ./log/
You can set use switch -cp or -classpath with javac command.
for example javac -cp path and name of jat file or class file yourjavafile.java
create the class file using the compiler: javac NewLogFileds.java
create the jar file using the jar command: jar cvf stuff.jar NewLogFileds.class
You are correct that the program needs a main() function in order to run.
add:
public static void main(String args[]) {
// code here
}
With that you could run the code with or without the jar:
java NewLogFields
or
java -cp stuff.jar NewLogFields
There are ways of creating a MANIFEST file that tells java which class to run from the jar making the last line more simple.
The link that you provided tells you how to do it:
Compile your class in the normal way.
Create a JAR file containing your class in the normal way.
Copy the JAR file into the wonza installation's lib as described in the javadoc.
Edit the startup script to add the -Dcom.wowza.wms.logging.LogNotify=... option to JAVA_OPTS ... as described in the javadoc.
The "full-path-to-your-ILogNotify-class" is actually supposed to be the fully qualified class name of your class; it is obvious from the examples.
Edit WowzaMediaServerPro-Service.conf and log4j.properties as described in the javadoc.
Restart the server.
If you put your JAR file in the directory like the instructions tell you to, you won't need to modify the classpath by changing -cp argument.
Your class doesn't need a main method because it is not a free-standing application. Rather, it is a "plugin" class that gets dynamically loaded and instantiated by the Wowza core as required. The "-D..." option and the config file change tell the Wonza core which class to try to load.