How to launch java application without having to specify classpath? - java

I'm new to java and I'm experimenting with hello world app.
I've exported the app from Eclipse into a jar, i DID specify the launch configuration, and when I ran it from command line, I retrieved
Error: Could not find or load main class
That can be fixed by specifying class path like this:
java -cp .:myjar.jar MyMainClass
However, this is really inconvenient. Is there any way, preferably through eclipse, to specify MyMainClass as a metadata inside the jar file so I don't have to write it down every time I launch the app?

yes, you can by writing a manifest file, and then running java -jar Yourjarfile.jar

Related

java Could not find or load main class from eclipse compiled code

I have compiled my java code using eclipse but not it has to be deployed and a cron job has to execute it. I am trying to execute it from command line in Windows, but getting Could not find or load main class. I tried setting classpath using java -cp bin\com\pega\download\engineclasses but it still throws the same error. My folder structure looks like below
C:\Users\s2517457\G360_Linux\FiddlingPega
|__\bin\com\pega\download\engineclasses\TestUtils.class
|__\src\com\pega\download\engineclasses\TestUtils.java
Please let me know what should be the javac and java commands for this to work.
You should use the following command:
java -cp bin/ com.pega.download.engineclasses.TestUtils
Your are telling to java that the entire bin folder is your classpath and the main class is in the class com.pega.download.engineclasses.TestUtils
If you want to add jars as well, you must call the command like:
java --classpath "bin/;lib/*" com.pega.download.engineclasses.TestUtils
Where lib is the folder containing the Jars files

Can not start Java application via commandline

So i have a maven project in eclipse that i can normally start via run as> Java Application. If i however try to start the class file from the target directory from the commandline via "java TestServer" it wont work. It says it cant find or load the main class. If i replace the sourcecode with a simple dummy hello world and execute then it works fine.
The code has two maven dependencies.
Simple dummy:
public class HelloWorld {
public static void main(String[] args){
System.out.println("hello world!");
}
}
Why does this happen? Is there a problem due to the nestest class? Why can't a main class be found for the code but for a small dummy?
The problem is the classpath.
Maven is not responsible for running the project from Eclipse workspace
because Eclipse holds its own .project file, which contains all classpath entries. When you use Run As, Eclipse just uses all classpath configurations of its .project file.
To start the java program from console, you need to set the classpath to your bin or target directory and to all libraries ('jar') which are referenced by your project. After setting the classpath correctly you can start your program with java <qualified classname>. Starting the program this way does not use any of Maven's functionallity yet at all.
Have a look here to use Maven https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
Maven will create a library jar which contains all classes of your project.
You can set the classpath to the generated library and start your programm using java <qualified classname>
EDIT due to comment
Here is an example for setting the classpath using the Windows OS
console. You can put this line also into an appropriate Windows batch (.bat)
file and start your program,
set classpath = .;log4j.jar;lib/any-other-lib.jar
java org.<whatever>.MyProgram
for further information for setting the classpath on other OS's you may also have a look at
setting Java Classpath in linux?

Setting java classpath only when compiling

Is it possible to set the class path only when using javac but then run the program without specifying the class path at java?
For example,
I am using ant that does the compiling for me and it sets the class path to ./:lib/swingx-all-1.6.3.jar. As far as I know, this basically compiles/uses javac with that class path.
So is it possible to then run my program with just java MainApp, i.e. without having to use a class path variable? Or is it only possible to run my program by using the ant command again?
You can bundle your application in a jar file. Then include your classpath in manifest.
Here are Ant manuals for jar and manifest tasks.
Then, to run your application:
$ java -jar MainApp.jar
If you can use ant to run your program, look at the java task. Create a new target named "run" or something like that.
You don't need to set the classpath environment variable; you can always supply a classpath manually with the -cp option.

How to run Java applications without using an IDE or the command prompt

This is more curiosity than a problem:
I was recently wondering if there was a way to run compiled Java applications without using the cmd or an IDE such as Eclipse. I use Eclipse, but it isn't very useful if you want to run the program independently. Can you save Java files in Windows Explorer so you can create a shortcut for them? If so, how? Is there some sort of special extension to the file? I've heard of .JAR files, but I'm not sure what they are. Can anyone tell me how to do it?
.JAR files are archives containing - amongst other things - your compiled classes and a manifest file. You may set the main entry point of your application in that manifest. See Setting an Application's Entry Point.
Normally if you double click a jar file in windows it will be opened by javaw.exe -jar <yourFile.jar>. javaw.exe will lookup the manifest and try start the main class defined there.
create the jar file for java application using following syntax jar -cvf .jar . then use javaw.exe -jar

Processbuilder-opening java script in linux

Back again, this time with a java question. I was told how to get Processbuilder to run nonnative scripts (calling the program that would run the script), but I've been trying to run a java script and have run into a couple problems. First off, should I use a .class or .jar? both of these can be run but I'm not sure which one of them will work better. And then how do I execute them correctly? I've tried calling java (/usr/lib/jvm/java-6-openjdk/jre/bin/java) and then giving the filepath to the class file, but that doesn't seem to work.
Any ideas?
I agree with sarnold in terms of the .jar question. In terms of executing code using ProcessBuilder, you can execute a .jar file as long as this file contains a main() method, and has the Main-Class manifest header, which can be generated when the .jar is created. Once you have the .jar created, you'd use a command like this to run the .jar:
java -jar jar_file_name_here.jar
If you have multiple main classes and you want to run a specific one, you could use a command like this:
java -jar jar_name.jar com.main.class.package.path.here.SomeClassName
Are you trying to execute someone elses .jar, or is it one of your own that you just want to be executed inside a script? Why are you using a script, out of curiosity?
You either need -jar and the pathname of a jar that has a manifest that names your main class, or -cp with the pathname of a directory that has your classes in it in the standard layout, or -cp with the pathname of a jar followed by the name of the class with a main.
java -jar I_AM_A_JAR_WITH_A_MANIFEST.jar
java -cp I_AM_JAR_1.jar:I_AM_JAR2.jar... this.is.my.FooClass
java -cp dir_path1:dir_path2:dir_path3 this.is.my.FooClass
where the 'dir_pathN' is a dir with standard class hierarchy.

Categories

Resources