How do to run java class in command Line (cmd)? (use classpath) - command-line

I have java class in folder D:\myProjects\new_example:
package new_example;
import com.google.gson.Gson;
class MyClass{
public static void main(String[] args) {
MyClass myClass = new MyClass();
Gson gson = new Gson();
System.out.println(gson.toJson(myClass.getMyDate()));
}
public String getMyDate(){
return "Hello";
}
}
How do to run this class in command Line (cmd) from disk D:? (If gson-2.2.4.jar is located: D:\library\gson-2.2.4.jar AND MyClass.java in D:\myProjects\new_example\MyClass.java), use classpath... How do to run it..?

All you need:
en.wikipedia.org/wiki/Classpath_(Java)

I would suggest trying this:
java -cp "jar_name.jar;libs/*" com.test.App
jar_name.jar : your jar name with .jar extension
libs/* : relative path to your dependency jars
com.test.App : Class with main(String[]) method

Try this from the directory D:\myProjects:
java -cp D:\library\gson-2.2.4.jar new_example.MyClass

Related

Eclipse VM arguments picking directory name without separator

I am setting the VM arguments in eclipse as -DFilePath="C:\file\txt"
But while calling this #FilePath# in java it is giving output as C:filetxt instead of C:\file\txt. This is resulting in file not found exception. Can anyone please help me on this..
The problem must be in how you are "calling this #FilePath#".
I tested with following code:
package test;
import java.io.File;
public class EnvPath {
public static void main(String[] args) {
String path = System.getProperty("FilePath");
System.out.println("Prop: " + path);
File file = new File(path);
System.out.println("File: " + file);
}
}
Started from Eclipse, as you described, or with java -DFilePath="C:\file\txt" test.EnvPath using Windows Command Prompt and using GNU bash - it always produces:
Prop: C:\file\txt
File: C:\file\txt

Set FopFactoryBuilder baseURI to jar classpath

I'm upgrading an Apache FOP 1.0 project to Apache FOP 2.1. In this project, all necessary files are packaged within the jar file.
I've added the new FopFactoryBuilder to generate a FopFactory
FopFactoryBuilder builder = new FopFactoryBuilder(new File(".").toURI());
builder = builder.setConfiguration(config);
fopFactory = builder.build();
but all my resouces are loaded from the relative path on my file system, not from the jar. How can I set the baseURI to the jar's classpath?
Thanks
We also used FOP 2.1 and want to achieve, that images inside jars-classpath will be found. Our tested and used solution is the following:
Create your own ResourceResolver
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import org.apache.fop.apps.io.ResourceResolverFactory;
import org.apache.xmlgraphics.io.Resource;
import org.apache.xmlgraphics.io.ResourceResolver;
public class ClasspathResolverURIAdapter implements ResourceResolver {
private final ResourceResolver wrapped;
public ClasspathResolverURIAdapter() {
this.wrapped = ResourceResolverFactory.createDefaultResourceResolver();
}
#Override
public Resource getResource(URI uri) throws IOException {
if (uri.getScheme().equals("classpath")) {
URL url = getClass().getClassLoader().getResource(uri.getSchemeSpecificPart());
return new Resource(url.openStream());
} else {
return wrapped.getResource(uri);
}
}
#Override
public OutputStream getOutputStream(URI uri) throws IOException {
return wrapped.getOutputStream(uri);
}
}
Create the FOPBuilderFactory with your Resolver
FopFactoryBuilder fopBuilder = new FopFactoryBuilder(new File(".").toURI(), new ClasspathResolverURIAdapter());
Finally address your image
<fo:external-graphic src="classpath:com/mypackage/image.jpg" />
Because you use our own Resolver it is possible to do every lookup which you want.
By specifying the URL as a classpath URL like:
<fo:external-graphic src="classpath:fop/images/myimage.jpg"/>
In this example the file is a resource in the resource-package fop.images but the actual file gets later packed to some entirely different place inside the JAR, which is - however - part of the classpath, so the lookup as above works.

Powermockito mock operation on hierarchy level class

My app has hierarchy level class objects as follows.
Package com.sample.folder1;
Public class ParentClass{ }
Package com.sample.folder2;
Public class Childclass1 extends ParentClass{ }
Unit piece under test package:
#Test
Public void testMocking{
Childclass1 obj = Powermockito.mock(Chilclass1.class);
}
When I execute above junit in eclipse it throws
"VerifyError: Inconsistent stackmap frames....."
Please suggest mocking on hierarchy classes on same and different packages.
Try to add -noverify (Java 8) or -XX:-UseSplitVerifier (Java 7) as vm parameter.

The java code can compile in cmd window, but can not run in eclipse

this is the code from TIJ4#
The java code can compile and run in cmd window , but can not compile and run in eclipse .
//: io/MemoryInput.java
import java.io.*;
public class MemoryInput {
public static void main(String[] args)
throws IOException {
StringReader in = new StringReader(
BufferedInputFile.read("MemoryInput.java"));
int c;
while((c = in.read()) != -1){
System.out.print((char)c);
}
}
the wrong information about the code in eclipse is :
BufferedInputFile cannot be resolved
BufferedInputFile is not part of the package java.io. If you have that class in a library or in a certain folder you have to include it in Eclipse.
BufferedInputFile is not part of any default lib of java. So you have to add that class to your class path.

Cannot use java value as parameter to java annotation on scala function

I have 3 java files: HW.java, myAnn.java, and Constants.java in package myApp.
Constants.java:
public final class Constants {
public static final String WORLD ="World";
}
myAnn.java:
public #interface myAnn {
java.lang.String name() default "";
}
HW.java:
class HW {
#myAnn(name = Constants.WORLD)
public static void main(String[] args){
System.out.println("Hi "+ Constants.WORLD);
}
}
My app compiles and runs fine as shown above, but I want to migrate HW.java to scala as
HelloWorld.scala:
object HelloWorld {
#myAnn(name = Constants.WORLD)
def main(args: Array[String]) {
println("Hello " + Constants.WORLD)
}
}
When I try to compile this, I get
error: annotation argument needs to be a constant; found:
Constants.WORLD #myAnn(name = Constants.WORLD)
If I remove the annotation then HelloWorld compiles and executes as expected.
Why can I use Constants.WORLD as a parameter to an annotation from a java program, but not from a scala program? Is there something I can modify in Constants.java to allow it to be used from either java or scala? I can't modify MyAnn.java, and I can't migrate Constants.java yet.
It is a bug that only shows up when feeding the java source files into the scala compiler, see issue SI-2764. The example works when compiling the java files first using javac and then pointing scalac's classpath to the generated classfiles.