• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

How can I convert strings to executable Java code with the Java compiler?

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
I am not a Java developer but I am interested in this language. I try to write NetCDF file in java with and . The commands for declaring variables and add them to ncfile are stored in a String. For example String Variable contains strings such as: . I have to convert these strings to java code. I tried to write a Java Compiler to compile these strings based on http://www.java2s.com/Tutorial/Java/0120__Development/CompileString.htm

Here is the part of my code where I want to convert the string with compiler and the Compiler code what I wrote.



and the Compiler



I got an error message:

If I am right the compiler does not understand the identifiers of ucar.nc2 and ucar.ma2. Could someone suggest me a way fixing this issue?
 
Saloon Keeper
Posts: 7627
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that the compiler can handle Java source code stored in a string - I think it needs files on disk to work with.

See https://stackoverflow.com/questions/12173294/compile-code-fully-in-memory-with-javax-tools-javacompiler for some approaches of how to tackle this. Other people have already done the hard work for you :-)
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried also the InMemoryJavaCompiler from https://github.com/trung/InMemoryJavaCompiler but I got the same errors. I do not understand that the compiler why does not understand the NetcdfFileWriter identifiers even though I imported and packages into the NetCDF_writer Class. Could someone please write to me how I should fix this issue?


 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show us the code that the compiler is supposed to be compiling?
And the errors it is giving back (I presume it provides some feedback, maybe an exception?).

You might want to log the value of sourceCode, to make sure you show us exactly what is being compiled rather than what you think is being compiled.
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understood well you need to know what the Strings contain.
String Variable contains the following strings:


String DataA contains:


String DataD contains:


String DataNC contains:


One of the error messages is:

Any other cannot find symbol messages point to the NetcdfFileWriter identifiers such as , ,

I do not understand why because of the ucar.nc2 and ucar.ma2 packages are imported into NetCDF_writer Class which contains these Strings.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, not quite.
This line is the compiler:


I want you to log (System.out.println(sourceCode) would do) the value of sourceCode before that line runs.

That way you know exactly what is being compiled.

That aside, the error tells you:
/meteoread/NetCDF_writer.java:3: error: cannot find symbol
  static void getDataNc(int id1, int id2, String listString, String location, String Stimenc, String ETimenc)throws Exception { Variable time;
                                                                                                                                ^


Variable is not a keyword in Java (unless you have a class called Variable).
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Dave,
The System.out.println(sourceCode) results:

package meteoread;
public class NetCDF_writer {
  static void getDataNc(int id1, int id2, String listString, String location, String Stimenc, String ETimenc)throws Exception { Variable time;
time = ncfile.addVariable(null, "time", DataType.DOUBLE, "time"); time.addAttribute( new Attribute("units", "days since 1970-01-01"));
Variable WD010;
WD010 = ncfile.addVariable(null, "WD010", DataType.DOUBLE, "time"); WD010.addAttribute(new Attribute( "long_name ", " wind direction")); WD010.addAttribute(new Attribute("unit", " degree")); WD010.addAttribute(new Attribute("_FillValue", " -999.9"));
ncfile.create();
ArrayDouble.D1 timeData = new ArrayDouble.D1(countLinesResult); Index ima = timeData.getIndex();
ArrayDouble.D1 WD010Data = new ArrayDouble.D1(countLinesResult);
for (int timeIdx = 0; timeIdx < countLinesResult; timeIdx ++){timeData.setDouble(ima.set(timeIdx),ValueD.get(timeIdx));
timeData.setDouble(ima.set(timeIdx),ValueD.get(timeIdx));
timeData.setDouble(ima.set(timeIdx),ValueD.get(timeIdx));
timeData.setDouble(ima.set(timeIdx),ValueD.get(timeIdx));
timeData.setDouble(ima.set(timeIdx),ValueD.get(timeIdx));
WD010Data.set(ima.set(timeIdx), ValueD.get(timeIdx));
WD010Data.set(ima.set(timeIdx), ValueD.get(timeIdx));
WD010Data.set(ima.set(timeIdx), ValueD.get(timeIdx));
WD010Data.set(ima.set(timeIdx), ValueD.get(timeIdx));
WD010Data.set(ima.set(timeIdx), ValueD.get(timeIdx));
}ncfile.write(time, timeData);
ncfile.write(WD010,WD010Data );
}}

If I type Strings like and the NetcdfFileWriter will not understand it because they are strings. If I type what the sourceCode produces NetcdfFileWriter will understand it and the will be: double time(time=0);

This is what I do not understand. The meteored package which contains my NetCDF_writer Class has libraries: netcdfAll-4.6.jar and toolsUI-4.5.6.jar and the necessary packages are imported into NetCDF_writer Class. That's why I don't understand this issue. Is it possible that the InMemoryJavaCompiler should contain ucar.nc2 and ucar.ma2 packages to understand the Variable Class (https://www.unidata.ucar.edu/software/netcdf-java/v4.3/v4.3/javadoc/ucar/nc2/Variable.html)?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so Variable is a class in those packages, so yes...you do need to import the packages, not in the class that is doing this compiling (NetCDF_writer ), but into this "temporary" class.

So the output from println for sourecCode should start:


That's what's missing, and is why the compiler cannot find the Variable class.
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Dave, Thank you so much for your suggestion. I tried it. It reduced the error messages, but it does not create a netCDF file even thought that the code is correct.
The System.out.println(sourceCode) is:



I do not get any error message, but it does not work. I built the compiler in ComPiler Class static void CodeFromString() inside meteoread package and
I called it inside NetCDF_writer Class. Is it possible
that I use this compiler in wrong way? If yes how should I apply it?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since I have no idea what it is you are trying to do with this code, I'm not sure I can help.

From the original code you posted earlier you compile some String containing Java code.
I'm not sure what you expect to happen to that code.
I don't see anywhere it is actually executed?
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Dave, I have a meteoread package which contains several Java Classes and Java FrameWorks.
Only one FrameWork is runnable where the whole package is built.
The NetCDF_writer Java Class is where I would like to write a netCDF file but the java codes for NetcdfFileWriter are stored in Strings.
I have to convert them to java code. It is possible with InMemoryCompiler. The first idea I had was to convert only the strings such as
Variables what I wrote before. It was not possible because the compiler needs to know the package, the Class,
the dependencies (e.g. ucar.nc2). Therefore I wrote a new Class ComPiler Class where the InMemoryJavaCompiler was built such as


The sourceCode contains the whole NetCDF_writer Class in strings and it produces what I posted before. I called the ComPiler Class
in the runnable FrameWork such as
When I run the package I do not get any error message but I guess the output of compiler will not execute.
I would like to know how I can execute the output of InMemoryJavaCompiler?
I hope someone can help me!
 
Tim Moores
Saloon Keeper
Posts: 7627
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that you have a Class object for meteoread.NetCDF_writer, you can call its newInstance() method to instantiate it. Then you can invoke any of its methods that you like. You will need to do this via reflection, though, since the Java code will have no knowledge of what methods the class declares. To avoid this, have the class implement a known interface that defines all the methods you want to call - then the Java code that uses it can cast the newly instantiated object to that interface, and you can easily call the methods.

I have in the past used the Javassist library for purposes like this, which works a bit differently, but can do what you're trying to achieve.
 
Marshal
Posts: 79671
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you still allowed to use newInstance()? That link suggests an alternative because of problems with exception handling.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This does strike me as a rather complex task for someone who describes themselves as "not a Java developer".
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your response! I tried the instantiate CodeFromString() method of ComPiler Class
where the InMemoryJavaCompiler id built by the following way:



Unfortunately, did not change anything. Thank you Tim for suggesting Javassist. I will check it.
 
Tim Moores
Saloon Keeper
Posts: 7627
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not instantiating anything. A class is what you need to instantiate, namely the one returned by the compiler. This one:

Class<?> Netcdf_writer = InMemoryJavaCompiler.compile("meteoread.NetCDF_writer", sourceCode.toString());

Did you understand my previous post about instantiation, reflection and why creating an interface will likely be necessary?
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Tim,
Sorry I misunderstood. I modified the ComPiler Class with  

In the JavaFrame where I would like to run the code I used ClassLoader to load the compiled Class and I tried to invoke it by
the suggested method in https://stackoverflow.com/questions/30430056/running-a-compiled-java-class-file-from-within-java-code-and-capturing-output
but I got an error after I run it.
I did the followings:

I got an error:
java.lang.NoSuchMethodException: meteoread.NetCDF_writer.main([Ljava.lang.String;)
What did I wrong?
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also tried it with:


because main method does not exist in NetCDF_writer Class.
But I got the same error.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the method you are trying to run:

I don't see where in your execution code you are supplying any suitable parameters.
 
Tim Moores
Saloon Keeper
Posts: 7627
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a number of problems in this code.

Class cls = Class.forName("className");


This should be Class.forName(className) - you don't want to use the string literal "className", you want to use the parameter className. But even then it wouldn't work, because the JVM does not know that class. As I've said before you need to use this class:

Class<?> Netcdf_writer = InMemoryJavaCompiler.compile("meteoread.NetCDF_writer", sourceCode.toString())

and then

Object instance = Netcdf_writer.newInstance()

ClassLoader cLoader = cls.getClassLoader();


The classloader is not needed, as witnessed by the fact that you are not actually using it.

Method main = cls.getDeclaredMethod("main", String[].class);


As you have found out, the class has no such method.

Method getDataNC = cls.getDeclaredMethod("getDataNc", String[].class);


As Dave pointed out, you need to match the exact types of the parameters. This piece of assumes a single String[] parameter - which is very different from the actual getDataNc method.

You can make it much easier for yourself by implementing that method as in interface, let's call it DataNcSource. If class Netcdf_writer then implements that interface, you can do the following instead of what I posted above:

DataNcSource instance = (DataNcSource) Netcdf_writer.newInstance()

and then simply call the method directly in your Java code, instead of going through the whole reflection mess.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, since the method is static then there's no need to use newInstance at all, since you don't need an instance to run the method.
Just use NetCDF_writer.getDataNc(...parameters go here...).
 
Tim Moores
Saloon Keeper
Posts: 7627
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:NetCDF_writer.getDataNc(...parameters go here...)


I don't think that would work, because "NetCDF_writer" is a Class object, and since the JVM does not know about the underlying class at compile time, you can't refer to it statically.
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Tim and Dave,
I tried your suggestions. I corrected the mentioned mistakes, but I gave some error messages.

First I tried also Tim's suggestion to use DataSource in ComPiler Class:


I tried to call it in the Java Frame jComboBoxActionPerformed:

       
      I do not know if I misunderstood your suggestion simply calling the method.
I got the error message: java.lang.ClassCastException: meteoread.NetCDF_writer cannot be cast to javax.activation.DataSource
most probably owing to the jdbc connection inside the NetCDF_writer https://access.redhat.com/solutions/2877761.

Second I tried to use:


and call it:

   

I got the error message: java.lang.UnsupportedOperationException: Not supported yet. (It may be also due to the connection).

I also tried to skip the .newInstance() :

and call it:


         

   I did not get any error but it did not execute NetCDF_writer.  
   


         
 
Tim Moores
Saloon Keeper
Posts: 7627
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

java.lang.ClassCastException: meteoread.NetCDF_writer cannot be cast to javax.activation.DataSource

most probably owing to the jdbc connection inside the NetCDF_writer



No. Re-read carefully what I wrote earlier. I said to create your own interface. The existing DataSource interface has nothing to do with this.

Object instance = NetCDF_writer.newInstance();

I got the error message: java.lang.UnsupportedOperationException: Not supported yet.


Of course - Object does not have that method. That is why you need to cast the instance to an interface that does have this method - namely, the one that you need to create.

NetCDF_writer.getDataNc(id1, id2, listString, location, STime, ETime);


I don't see how that could even compile.
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Tim,
Thank you for your response! I do not really understand what you suggested concerning interface.
I can create a DataNcSource interface but I don't understand which kind of method should I use?
If I know well the methods in interfaces have to be empty bodies. Did you think the method
private String invokeClass(String className)? It is not an empty body method.
Moreover, I do not know how should I implement the method in an interface in Netcdf_write Class
which was generated by InMemoryJavaCompiler inside ComPiler Class?




 
Tim Moores
Saloon Keeper
Posts: 7627
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I can create a DataNcSource interface but I don't understand which kind of method should I use?


If you have an object of type NetCDF_writer, which method are you trying to call on it? That's the one that goes in the interface.

I do not know how should I implement the method in an interface in Netcdf_write Class which was generated by InMemoryJavaCompiler inside ComPiler Class?


The interface does not implement the method. The interface merely declares the method signature (with a non-existing body). The implementation of the method is in the class compiled by InMemoryJavaCompiler. You already have that part; the only thing mising is for that class to declare that it implements the interface.

If you're uncertain about declaring and using interfaces, start by working through https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html
 
Tim Moores
Saloon Keeper
Posts: 7627
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is just about the simplest possible scenario where a class is compiled that implements an interface. I hope that makes things clearer.

Class org.mdkt.SaysHello



Class org.mdkt.HelloTest

 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Tim! Thank you so much for your response and help!
I tried it but I got again
java.lang.ClassCastException: meteoread.NetCDF_writer cannot be cast to meteoread.DataNcSource
I try to write NetCDF_writer Class with only strings without sql connection. Maybe it
will help to avoid this error message.
 
Tim Moores
Saloon Keeper
Posts: 7627
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I try to write NetCDF_writer Class with only strings without sql connection. Maybe it will help to avoid this error message.


It has nothing to do with any SQL code. You just seem to be making random changes in order to get something to work. That is not a good approach to such a problem (or any problem, really).

What changes have you made in order to have the NetCDF_writer implement your interface? Also, post the contents of the interface file in full, so we can tell whether you're on the right track.
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Tim,
The Test Class avoided my attention. I created one
interface:



and one Netcdf_writer_Test Class:




After I run the code (the JavaFrame which is runnable) I did not get any error but I did not get any netcdf file. Furthermore
I added some System.out.println into the strings which are compiled and I did not get any
output. I do not know what did I wrong.
 
Tim Moores
Saloon Keeper
Posts: 7627
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I did not get any netcdf file.


Do you mean the class file? The in-memory compiler doesn't produce one.

I did not get any output


What output are you expecting? The code you posted creates no output, nor are you calling the getDataNC method. Even if you did, the getDataNC method in Netcdf_writer does precisely nothing - it's empty. Looking closely, I don't think it would even compile, because the getDataNC method is missing opening and closing braces. I'm surprised that does not cause an exception.
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Tim,
No I mean the produced netcdf file via
executing Netcdf_writer. I corrected the missing bracket
but it did not help.
The whole story is that I have a JavaFrame which is runnable
and if I run the program it will launch every Java Class and JFrame via ActionPerformed.
I have a NetCDF_writer Class where some data from sql database
will write in netCDF format. The input parameters for sql data selecting
are sent from runnable JFrame by


In the NetCDF_writer Class the a java codes for NetcdfFileWriter are stored in Strings.
Therefore the whole NetCDF_writer Class is compile with ComPile Class CodeFromString()
where the InMemoryJavaCompiler compile the sourceCode (the whole NetCDF_writer Class in strings).
Then the compiler will create a Netcdf_writer Class which is the NetCDF_writer in java code.
The Netcdf_writer_Test will execute the NetCDF_writer  getDataNc method.
Maybe the matter is that I do not know how should I launch the compiler and then the
Netcdf_writer_Test Class to be executed.
I tried with:


but I have a doubt that I did it well.                                
     

 
Tim Moores
Saloon Keeper
Posts: 7627
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure how to help any further. Lines 13, 14 and 15 in the example I posted show how to create (by compiling) a Class object from a source string, how to instantiate the class object  and cast it to an interface, and how to invoke a method on that object.

Your code doesn't do anything like that. It seems to invoke the compiler in line 37, but it calls the method in line 36 - before the compilation has created the class. And there is no code that instantiates and casts the object. How do you expect that to work?

My advice would be to delete the ComPiler class, and move all of the code in the CodeFromString method directly to where you will use it (somewhere around line 33). That way it's easier to compare it with the example code of mine.
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Tim,
Thank you for your lot of help! I partly solved the matter. I made the changes:

The output of ComPiler Class CodeFromString() is


as it was.

The DataNcSource Interface is as I posted earlier.

I changed the Netcdf_writer_Test Class as:

and I call it in runnable JavaFrame as:

but I got the error messages:
/meteoread/Netcdf_writer.java:2: error: '{' expected
public class Netcdf_writer implement DataNcSource{
                         ^
1 error
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: meteoread/Netcdf_writer (wrong name: meteoread/NetCDF_writer)
at java.lang.ClassLoader.defineClass1(Native Method)
I do not understand why I need a { braket and what is the matter with Netcdf_writer? The NetCDF_writer Class was compiled by InMemoryJavaCompiler.
 
Saloon Keeper
Posts: 10879
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"implement" should be "implements"
Not sure if that is your problem but that is a problem.
 
Marshal
Posts: 28293
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Beata Szabo-Takacs wrote:what is the matter with Netcdf_writer? The NetCDF_writer Class was compiled by InMemoryJavaCompiler.



What is the matter with Netcdf_writer? The class you're looking for is named NetCDF_writer, not Netcdf_writer. Those are two different names as far as Java is concerned.
 
Tim Moores
Saloon Keeper
Posts: 7627
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, but it seems the code has become worse - it's now divided into 3 sections: the CodeFromString method, the Netcdf_writer_Test class and the code in the jComboBox2ActionPerformed method. I can only repeat what I said before: put it all into a single location so that you actually see what it is you're coding. It still looks like you're just copy/pasting bits and pieces of code together in the hope that it will magically work. But it won't. Lines 30-34 of jComboBox2ActionPerformed make no sense, and don't contain any of the fixes I mentioned in my last post.

Very little is actually missing at this point, but you need to stop and think how the pieces fit together.
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Tim,
Thank you for your response! I tried to solve this issue with your suggestion.
I created only one source. This is the ComPiler() Class where the In MemoryJavaCompiler will compile the NC_writer class.
(I changed the class name and I will delete the NetCDF_writer Class).
The ComPiler() Class contains the



and the interface is:



I just called it in jComboBox as:


But I got the following error:
/meteoread/NC_writer.java:26: error: NC_writer is not abstract and does not override abstract method getDataNC() in DataNcSource
public class NC_writer implements DataNcSource {
      ^
1 error

If I changed NC_writer to abstract NC_writer inside sourceCode and run the code
I got the following error:
java.lang.IllegalAccessException: Class meteoread.ComPiler can not access a member of class meteoread.NC_writer with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
at java.lang.Class.newInstance(Class.java:436)






 
Tim Moores
Saloon Keeper
Posts: 7627
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to pay attention to details: getDataNc is not the same as getDataNC. As Paul pointed out earlier, capitalization matters in Java.
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to thank again everyone for a lot of help! The issue is solved based on your suggestions.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic