XStream implicit configouration - java

I am working with XML like:
<localMSZ>
<territories>
<codeOKTMO>str1</codeOKTMO>
<codeOKTMO>str2</codeOKTMO>
</territories>
</localMSZ>
In Java code I have class LocalMSZ which have List of String like:
class LocalMSZ {
List<String> territories;
}
I doesn't understand how I should post annotation in this case?

The problem is in your mapping class: it lacks the structure and annotations needed for this. It should work with this:
import java.util.LinkedList;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;
#XStreamAlias("localMSZ")
public class LocalMSZ {
private Territories territories = new Territories();
public Territories getTerritories() {
return territories;
}
public void setTerritories(Territories territories) {
this.territories = territories;
}
#XStreamAlias("codeOKTMO")
#XStreamConverter(value = ToAttributedValueConverter.class, strings = { "value" })
public static class Code {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
#XStreamAlias("territories")
public static class Territories {
// This one maps the sequence of <codeOKTMO> tags
#XStreamImplicit
private List<Code> codes = new LinkedList<Code>();
public List<Code> getCodes() {
return codes;
}
public void setCodes(List<Code> codes) {
this.codes = codes;
}
}
}
Remember also when you write your main method to process the annotations of LocalMSZ
XStream xstream = new XStream();
xstream.processAnnotations(LocalMSZ.class);
...

Related

How to make Jackson to inject the Generic Class into a bean?

Lets suppose, that we have a bean like this:
public class Response<T> {
private T data;
private double executionDuration;
private boolean success;
private String version;
//HOW TO Make Jackson to inject this?
private Class<T> dataClass;
public Optional<T> getData() {
return Optional.ofNullable(data);
}
public double getExecutionDuration() {
return executionDuration;
}
public Class<T> getDataClass() {
return dataClass;
}
public String getVersion() {
return version;
}
public boolean isSuccess() {
return success;
}
}
The deserialization happens like this:
objectMapper.readValue(json, new TypeReference<Response<SomeClass>>() {});
Can I somehow make Jackson to inject the class "SomeClass" into my bean? Injecting the type reference itself would be also ok, I think.
If it is undesirable to save class info in json and use #JsonTypeInfo I would suggest to use #JacksonInject:
public class Response<T> {
private T data;
private double executionDuration;
private boolean success;
private String version;
#JacksonInject("dataClass")
private Class<T> dataClass;
public Optional<T> getData() {
return Optional.ofNullable(data);
}
public double getExecutionDuration() {
return executionDuration;
}
public Class<T> getDataClass() {
return dataClass;
}
public String getVersion() {
return version;
}
public boolean isSuccess() {
return success;
}
}
Deserialization would look like:
ObjectMapper mapper = new ObjectMapper();
InjectableValues.Std injectable = new InjectableValues.Std();
injectable.addValue("dataClass", SomeClass.class);
mapper.setInjectableValues(injectable);
final Response<Integer> response = mapper.readValue(json, new TypeReference<Response<SomeClass>>() { });
this worked for me;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
#Getter
#Setter
#NoArgsConstructor
public class Entity<T> {
private T data;
#JsonSerialize(converter = ClassToStringConverter.class)
#JsonDeserialize(converter = StringToClassConverter.class)
private Class<T> dataClass;
}
and
import com.fasterxml.jackson.databind.util.StdConverter;
public class ClassToStringConverter extends StdConverter<Class<?>, String> {
public String convert(Class<?> aClass) {
// class java.lang.Integer
return aClass.toString().split("\\s")[1];
}
}
and
import com.fasterxml.jackson.databind.util.StdConverter;
public class StringToClassConverter extends StdConverter<String, Class<?>> {
public Class<?> convert(String s) {
try {
return Class.forName(s);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
Main;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
Entity<Integer> data = new Entity<Integer>();
data.setData(5);
data.setDataClass(Integer.class);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(data);
Entity<Integer> jsonData = mapper.readValue(json, new TypeReference<Entity<Integer>>() {});
System.out.println(jsonData.getData());
System.out.println(jsonData.getDataClass().getCanonicalName());
}
}
But, maybe it will be better, to not save the class type, but use method to get type from data?
public Class<T> getType() {
return (Class<T>) data.getClass();
}
public class Response<T> {
private T data;
// other fields & methods
public Class getType() {
return Optional.ofNullable(data).map(Object::getClass).orElse(Void.class);
}
public Optional<Class> getSafeType() {
return Optional.ofNullable(data).map(Object::getClass);
}
}
Super simple, no need to tinker with Jackson, NPE safe...

Jaxb with baseclass doesn't serialize derived elements

I have a list of modules in a tree like structure. The baseclass contains the childs and some generic property, while the derived class should provide it's individual fields. Now when I try to serialize this with JAXB I get only the members of the baseclass but not the ones from the derived classes.
Application.java
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class Application
{
public static void main(String[] args)
{
try
{
Emulators emulators = new Emulators();
emulators.addChild(new VICEModule());
emulators.addChild(new VICEModule());
StringWriter sw = new StringWriter();
JAXBContext jaxbContext = JAXBContext.newInstance(emulators.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // Pretty print
jaxbMarshaller.marshal(emulators, sw);
String s = sw.toString();
System.out.println(s);
}
catch(Throwable e)
{
System.err.println("Exception:"+e);
}
}
}
ModuleBase.java
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import com.sun.xml.internal.txw2.annotation.XmlAttribute;
public class ModuleBase
{
private List<ModuleBase> mChilds = new ArrayList<>();
private String mModuleId;
private String mModuleName;
public ModuleBase(String oModuleId, String oModuleName)
{
setModuleId(oModuleId);
setModuleName(oModuleName);
}
public void addChild(ModuleBase oModuleNode)
{
mChilds.add(oModuleNode);
}
#XmlElement(name="ModuleList")
public List<ModuleBase> getChildModules()
{
return mChilds;
}
#XmlAttribute
public String getModuleId()
{
return mModuleId;
}
public void setModuleId(String oModuleId)
{
mModuleId = oModuleId;
}
#XmlAttribute
public String getModuleName()
{
return mModuleName;
}
public void setModuleName(String oModuleName)
{
mModuleName = oModuleName;
}
}
Emulators.java
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "Emulators")
public class Emulators
extends ModuleBase
{
public Emulators()
{
super("IdEmu129872q53", "Emulators");
}
}
VICEModule.java
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
#XmlRootElement(name = "VICEModule")
public class VICEModule
extends ModuleBase
{
private String mInstallationPath;
private int mPort;
public VICEModule()
{
super("Id123456", "Vice");
mPort = 6510;
}
//#XmlElement(name="InstallationPath")
#XmlValue
public String getInstallationPath()
{
return mInstallationPath;
}
public void setInstallationPath(String oPath)
{
mInstallationPath = oPath;
}
//#XmlElement(name="Port")
#XmlValue
public int getPort()
{
return mPort;
}
public void setPort(int nPort)
{
mPort = nPort;
}
}
Now when I serialize it, I get the following XML where the VICEModule values are missing and also the module is listed as ModuleList instead of VICEModule and the base fields are put as tags instead of attributes:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Emulators>
<ModuleList>
<moduleId>Id123456</moduleId>
<moduleName>Vice</moduleName>
</ModuleList>
<ModuleList>
<moduleId>Id123456</moduleId>
<moduleName>Vice</moduleName>
</ModuleList>
<moduleId>IdEmu129872q53</moduleId>
<moduleName>Emulators</moduleName>
</Emulators>
So what this should look like is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Emulators ModuleId="IdEmu129872q53" ModuleName="Emulators">
<VICEModule ModuleId="Id1" ModuleName="Name">
<InstallationPath>Path1</InstallationPath>
<Port>6510</Port>
</VICEModule>
<VICEModule ModuleId="Id2" ModuleName="Name">
<InstallationPath>Path2</InstallationPath>
<Port>6511</Port>
</VICEModule>
</Emulators>
When I use the VICEModule as the baseclass, then the XML looks more like it (still without the attributes though).
Writing this small demo app helped, because now I could more easily play around with the marshalling and finally got it working. I'm posting the changes here, to make this sample complete in case somebody needs it as a reference. Emulators.java was not changed, so it's not listed in the answer.
Short answer:
I had to add #XmlAnyElement(lax=true) to my getter for the child modules in order to convert the resulting XML node into a named node as I want it to. Additionally in the main application I had to traverse the tree and collect all classes that are used, so that I can pass it to the JAXBContext.
Application.java
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class Application
{
public static List<Class<?>> createClasses(ModuleBase oModule)
{
List<Class<?>> classes = new ArrayList<>();
classes.add(oModule.getClass());
for(ModuleBase module : oModule.getChildModules())
{
List<Class<?>> cls = createClasses(module);
classes.addAll(cls);
}
return classes;
}
public static void main(String[] args)
{
try
{
Emulators emulators = new Emulators();
emulators.addChild(new VICEModule("Id1", "VICE V1", "V1 Path", 6510));
emulators.addChild(new VICEModule("Id2", "VICE V2", "V2 Path", 6511));
StringWriter sw = new StringWriter();
List<Class<?>> classes = createClasses(emulators);
Class<?>[] cls = new Class<?>[classes.size()];
for(int i = 0; i < classes.size(); i++)
cls[i] = classes.get(i);
JAXBContext jaxbContext = JAXBContext.newInstance(cls);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // Pretty print
jaxbMarshaller.marshal(emulators, sw);
String s = sw.toString();
System.out.println(s);
}
catch(Throwable e)
{
System.err.println("Exception:"+e);
}
return;
}
}
ModuleBase.java
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlAttribute;
public class ModuleBase
{
private List<ModuleBase> mChilds = new ArrayList<>();
private String mModuleId;
private String mModuleName;
public ModuleBase(String oModuleId, String oModuleName)
{
setModuleId(oModuleId);
setModuleName(oModuleName);
}
public void addChild(ModuleBase oModuleNode)
{
mChilds.add(oModuleNode);
}
#XmlAnyElement(lax=true)
public List<ModuleBase> getChildModules()
{
return mChilds;
}
#XmlAttribute
public String getModuleId()
{
return mModuleId;
}
public void setModuleId(String oModuleId)
{
mModuleId = oModuleId;
}
#XmlAttribute
public String getModuleName()
{
return mModuleName;
}
public void setModuleName(String oModuleName)
{
mModuleName = oModuleName;
}
}
VICEModule.java
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "VICEModule")
public class VICEModule
extends ModuleBase
{
private String mInstallationPath;
private int mPort;
public VICEModule()
{
super("Id123456", "Vice");
mPort = 6510;
}
public VICEModule(String oId, String oName, String oPath, int nPort)
{
super(oId, oName);
setInstallationPath(oPath);
setPort(nPort);
}
#XmlElement(name="InstallationPath")
public String getInstallationPath()
{
return mInstallationPath;
}
public void setInstallationPath(String oPath)
{
mInstallationPath = oPath;
}
#XmlElement(name="Port")
public int getPort()
{
return mPort;
}
public void setPort(int nPort)
{
mPort = nPort;
}
}
Now the XML renders exactly as intended with storing all the objects from derived classes.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Emulators moduleId="IdEmu129872q53" moduleName="Emulators">
<VICEModule moduleId="Id1" moduleName="VICE V1">
<InstallationPath>V1 Path</InstallationPath>
<Port>6510</Port>
</VICEModule>
<VICEModule moduleId="Id2" moduleName="VICE V2">
<InstallationPath>V2 Path</InstallationPath>
<Port>6511</Port>
</VICEModule>
</Emulators>

Populating Action class instance variables from URL in Struts2

Facing an issue with passing values from my html form to action class. Created a sample project to test the functionality and have the same issue here. I have the following classes:
TestBean
package com.struts2test.beans;
public class TestBean {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
TestBeanHolder
package com.struts2test.beans;
import java.util.List;
import java.util.Map;
public class TestBeanHolder {
private Map<Integer, TestBean> testBeanMap;
private List<TestBean> testBeanList;
private Map<Integer, List<TestBean>> testBeanListMap;
public Map<Integer, TestBean> getTestBeanMap() {
return testBeanMap;
}
public void setTestBeanMap(Map<Integer, TestBean> testBeanMap) {
this.testBeanMap = testBeanMap;
}
public Map<Integer, List<TestBean>> getTestBeanListMap() {
return testBeanListMap;
}
public void setTestBeanListMap(Map<Integer, List<TestBean>> testBeanListMap) {
this.testBeanListMap = testBeanListMap;
}
public List<TestBean> getTestBeanList() {
return testBeanList;
}
public void setTestBeanList(List<TestBean> testBeanList) {
this.testBeanList = testBeanList;
}
}
TestAction
package com.struts2test.action;
import com.opensymphony.xwork2.ActionSupport;
import com.struts2test.beans.TestBeanHolder;
public class TestAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private TestBeanHolder testBeanHolder;
public TestBeanHolder getTestBeanHolder() {
return testBeanHolder;
}
public void setTestBeanHolder(TestBeanHolder testBeanHolder) {
this.testBeanHolder = testBeanHolder;
}
public String execute() throws Exception {
return SUCCESS;
}
}
When my url is http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanMap[0].value=1, testBeanHolder.testBeanMap of my action gets populated with key of 0 mapping to a TestBean instance with value=1.
When the url is http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanList[0].value=1, testBeanHolder.testBeanList gets populated with single instance of TestBean with value=1.
I am try to populate testBeanListMap property of testBeanHolder and doesn't work. The testBeanListMap is created but empty. Here is the URL I am trying http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanListMap[0][0].value=1
Here is the code which worked, adding modified classes:
TestBeanListHolder
package com.struts2test.beans;
import java.util.List;
public class TestBeanListHolder {
private List<TestBean> testBeans;
public List<TestBean> getTestBeans() {
return testBeans;
}
public void setTestBeans(List<TestBean> testBeans) {
this.testBeans = testBeans;
}
}
TestBeanHolder
package com.struts2test.beans;
import java.util.List;
import java.util.Map;
public class TestBeanHolder {
private Map<Integer, TestBean> testBeanMap;
private List<TestBean> testBeanList;
private Map<Integer, TestBeanListHolder> testBeanListMap;
public Map<Integer, TestBean> getTestBeanMap() {
return testBeanMap;
}
public void setTestBeanMap(Map<Integer, TestBean> testBeanMap) {
this.testBeanMap = testBeanMap;
}
public Map<Integer, TestBeanListHolder> getTestBeanListMap() {
return testBeanListMap;
}
public void setTestBeanListMap(
Map<Integer, TestBeanListHolder> testBeanListMap) {
this.testBeanListMap = testBeanListMap;
}
public List<TestBean> getTestBeanList() {
return testBeanList;
}
public void setTestBeanList(List<TestBean> testBeanList) {
this.testBeanList = testBeanList;
}
}
URL
http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanListMap[1].testBeans[0].value=somevalue
The url http://localhost:8080/Struts2Test/test?testBeanHolder.testBeanListMap[0][0].value=1 won't work because you are using wrong parameter name. Thus testBeanHolder.testBeanListMap[0][0].value is a name of the parameter that maps to the object which has a property of complex type (collection of collections). Struts2 can't handle such scenarios, . But you can wrap a second collection with an object and use a collection of objects. The name would change to testBeanHolder.testBeanListMap[0].object[0].value.
The expression testBeanHolder.testBeanListMap[0][0].value is not a valid OGNL expression.
See here for a complete reference of what is allowed.

MusixMatch API and GSON: Using track.snippet.get instead of track.lyrics.get

I am working on the final project for an intro to Java class. Part of the project involves getting a lyric snippet from MusixMatch using their API. I am able to get lyrics from the API using track.lyrics.get, but cannot get snippets using tracks.snippet.get.
I started with a Java wrapper found here: https://github.com/sachin-handiekar/jMusixMatch and added my own classes to get a snippet based on the track.snippet.get API method.
When I run the program I get this error:
java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at
line 1 column 102 path $.message.body
My getSnippet method and applicable classes follow. They are based on the getLyrics method and classes found in the original wrapper.
public Snippet getSnippet(int trackID) throws MusixMatchException {
Snippet snippet = null;
SnippetGetMessage message = null;
Map<String, Object> params = new HashMap<String, Object>();
params.put(Constants.API_KEY, apiKey);
params.put(Constants.TRACK_ID, new String("" + trackID));
String response = null;
response = MusixMatchRequest.sendRequest(Helper.getURLString(
Methods.TRACK_SNIPPET_GET, params));
Gson gson = new Gson();
try {
message = gson.fromJson(response, SnippetGetMessage.class);
} catch (JsonParseException jpe) {
handleErrorResponse(response);
}
snippet = message.getContainer().getBody().getSnippet();
return snippet;
}
The Snippet Class
package org.jmusixmatch.entity.snippet;
import com.google.gson.annotations.SerializedName;
/**
* Created by kyledhebert on 4/30/15.
* Objects of this clas represent a lyric snippet from the
* MusixMatch API.
*/
public class Snippet {
#SerializedName("snippet_language")
private int snippetLanguage;
#SerializedName("restricted")
private int restricted;
#SerializedName("instrumental")
private int instrumental;
#SerializedName("snippet_body")
private String snippetBody;
#SerializedName("script_tracking_url")
private String scriptTrackingURL;
#SerializedName("pixel_tracking_url")
private String pixelTrackingURL;
#SerializedName("html_tracking_url")
private String htmlTrackingURL;
#SerializedName("updated_time")
private String updatedTime;
public int getSnippetLanguage() {
return snippetLanguage;
}
public void setSnippetLanguage(int snippetLanguage) {
this.snippetLanguage = snippetLanguage;
}
public int getRestricted() {
return restricted;
}
public void setRestricted(int restricted) {
this.restricted = restricted;
}
public int getInstrumental() {
return instrumental;
}
public void setInstrumental(int instrumental) {
this.instrumental = instrumental;
}
public String getSnippetBody() {
return snippetBody;
}
public void setSnippetBody(String snippetBody) {
this.snippetBody = snippetBody;
}
public String getPixelTrackingURL() {
return pixelTrackingURL;
}
public void setPixelTrackingURL(String pixelTrackingURL) {
this.pixelTrackingURL = pixelTrackingURL;
}
public String getScriptTrackingURL() {
return scriptTrackingURL;
}
public void setScriptTrackingURL(String scriptTrackingURL) {
this.scriptTrackingURL = scriptTrackingURL;
}
public String getHtmlTrackingURL() {
return htmlTrackingURL;
}
public void setHtmlTrackingURL(String htmlTrackingURL) {
this.htmlTrackingURL = htmlTrackingURL;
}
public String getUpdatedTime() {
return updatedTime;
}
public void setUpdatedTime(String updatedTime) {
this.updatedTime = updatedTime;
}
}
The SnippetGetBody class:
package org.jmusixmatch.entity.snippet.get;
import com.google.gson.annotations.SerializedName;
import org.jmusixmatch.entity.snippet.Snippet;
public class SnippetGetBody {
#SerializedName("snippet")
private Snippet snippet;
public Snippet getSnippet() {
return snippet;
}
public void setSnippet(Snippet snippet) {
this.snippet = snippet;
}
}
The SnippetGetContainer class:
package org.jmusixmatch.entity.snippet.get;
import com.google.gson.annotations.SerializedName;
import org.jmusixmatch.entity.Header;
public class SnippetGetContainer {
#SerializedName("body")
private SnippetGetBody body;
#SerializedName("header")
private Header header;
public SnippetGetBody getBody() {
return body;
}
public void setBody(SnippetGetBody body) {
this.body = body;
}
public Header getHeader() {
return header;
}
public void setHeader(Header header) {
this.header = header;
}
}
The SnippetGetMessage class:
package org.jmusixmatch.entity.lyrics.get;
import com.google.gson.annotations.SerializedName;
public class SnippetGetMessage {
#SerializedName("message")
private SnippetGetContainer container;
public void setContainer(SnippetGetContainer container) {
this.container = container;
}
public SnippetGetContainer getContainer() {
return container;
}
}
I was not able to reproduce your exact error message, but I did find the following error: snippet_language is a String, not an int. Change the type (and associated getters and setters) to:
#SerializedName("snippet_language")
private String snippetLanguage;
I used the sample Json response from here to make this work. If these two changes don't fix your problem, please edit your question with the actual Json response that is making your program not work.

Instantiating/Referencing static nested class inside static nested class

I have been give a jar file to use that has a static inner class inside of another static inner class:
package externalJarFile;
public class Job
{
public static class GlobalVars
{
private List<Job.GlobalVars.Variable> variables;
public List<Job.GlobalVars.Variable> getVariable()
{
if (this.variables == null) {
this.variables = new ArrayList<Job.GlobalVars.Variable>();
}
return this.variables;
}
public static class Variable
{
String name;
String value;
public String getName() { return name; }
public void setName( String name ) { this.name = name; }
public String getValue() { return value; }
public void setValue( String value) { this.value= value; }
}
}
}
The problem I'm having is that I need to populate the "Job.GlobalVars" list, but I can't figure out how to reference the "Variables" type. Whenever I add:
import externalJarFile.Job.GlobalVars.Variable;
I get a compilation error that the type "externalJarFile.Job.GlobalVars.Variable" cannot be referenced. How can I create a new "Variable" instance to add to the "GlobalVars.getVariable()" list?
Here's a snippet that I tried (but didn't work):
Job.GlobalVars vars = new Job.GlobalVars();
Job.GlobalVars.Variable v = new Job.GlobalVars.Variable();
[Edited for clarity]
[UPDATE]
Ok, this is kinda weird. If I take the code from the original project and directly import it into mine, I'm able to reference the inner-inner-class. However, when I reference it when it's packaged inside of a jar file, it fails. MTK...
You forgot a space:
Job.GlobalVars vars = new Job.GlobalVars();
^
This works fine for me:
Job.GlobalVars.Variable var = new Job.GlobalVars.Variable();
var.setName("MByD");
Class job:
package mypackage;
import java.util.ArrayList;
import java.util.List;
public class Job {
public static class GlobalVars
{
private List<Variable> variables;
public List<Variable> getVariable()
{
if (this.variables == null) {
this.variables = new ArrayList<Variable>();
}
return this.variables;
}
public static class Variable
{
String name;
String value;
public String getName() { return name; }
public void setName( String name ) { this.name = name; }
public String getValue() { return value; }
public void setValue( String value) { this.value= value; }
}
}
}
Other class using GlobalVars and Variable. Import works very good.
package mypackage;
import mypackage.Job.GlobalVars;
import mypackage.Job.GlobalVars.Variable;
public class RunIt {
public static void main(String[] args) {
GlobalVars vars = new GlobalVars();
Variable v = new Variable();
}
}
No need to import anything. You should be able to just refer to your inner class by its name, "Variable", from the "Job" class:
private List<Variable> variables;
public List<Variable> getVariable()
They way you had stated above is correct. You should check to ensure that the jar file is in your classpath as that would definitely cause the import to fail and subsequently all future declarations.
import mypackage.Job.GlobalVars.Variable;
...
Variable v = new Variable();

Categories

Resources